前提
キャラ移動・切り替えのスクリプトを書いていましたが、
以下のエラーメッセージが発生しキャラが移動しなくなりました。
該当のパラメーターが存在していないことはわかりましたが、具体的な解決方法がわからないので教えてほしいです。
発生している問題・エラーメッセージ
Parameter 'movespeed' does not exist. UnityEngine.Animator:SetFloat (string,single) player:ChangeControl (bool) (at Assets/Script/player.cs:78) ChangeChara:Start () (at Assets/Script/ChangeChara.cs:17)
該当のソースコード
using System.Collections.Generic; using UnityEngine; public class player : MonoBehaviour { private Animator anim = null; private Rigidbody rb; [SerializeField] private float movespeed = 10f; private bool control; // Start is called before the first frame update void Awake() { anim = GetComponent<Animator>(); rb = GetComponent<Rigidbody>(); } // Update is called once per frame void FixedUpdate() { if (control) { if (Input.GetKey(KeyCode.W)) { rb.AddForce(transform.forward * movespeed); anim.SetBool("run", true); } else if (Input.GetKey(KeyCode.S)) { rb.AddForce(-transform.forward * movespeed); anim.SetBool("run", true); } else if (Input.GetKey(KeyCode.D)) { rb.AddForce(transform.right * movespeed); transform.localScale = new Vector3(-1, 1, 1); anim.SetBool("run", true); } else if (Input.GetKey(KeyCode.A)) { rb.AddForce(-transform.right * movespeed); transform.localScale = new Vector3(1, 1, 1); anim.SetBool("run", true); } else { anim.SetBool("run", false); movespeed = 0.0f; } } } public void ChangeControl(bool controlFlag) { control = controlFlag; anim.SetFloat("movespeed", 0.0f); GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeAll; } }
using UnityEngine; using System.Collections; using System.Collections.Generic; public class ChangeChara : MonoBehaviour { // 現在どのキャラクターを操作しているか private int nowChara; // 操作可能なゲームキャラクター [SerializeField] private List<GameObject> charaList; void Start() { // 最初の操作キャラクターを0番目のキャラクターにする charaList[0].GetComponent<player>().ChangeControl(true); charaList[1].GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeAll; } void Update() { // Qキーが押されたら操作キャラクターを次のキャラクターに変更する if (Input.GetKeyDown("q")) { ChangeCharacter(nowChara); } } // 操作キャラクター変更メソッド void ChangeCharacter(int tempNowChara) { // 現在操作しているキャラクターを動かなくする charaList[tempNowChara].GetComponent<player>().ChangeControl(false); // 次のキャラクターの番号を設定 var nextChara = tempNowChara + 1; if (nextChara >= charaList.Count) { nextChara = 0; } // 次のキャラクターを動かせるようにする charaList[nextChara].GetComponent<player>().ChangeControl(true); // 現在のキャラクター番号を保持する nowChara = nextChara; } }
試したこと
最初はtransform移動でしたが、オブジェクトがすり抜けてしまうのが嫌でrb.AddForceに変えました。変える前は移動はできていました。
transform.position -= movespeed * transform.forward * Time.deltaTime;から
rb.AddForce(transform.forward * movespeed);に変えたところ、上記のエラーがでました。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2023/08/20 08:29