UnityのCharacterControllerに関するエラーです。既にCharacterControllerをアタッチしているのにアタッチしろと言われます。
MissingComponentException: There is no 'CharacterController' attached to the "Player" game object, but a script is trying to access it. You probably need to add a CharacterController to the game object "Player". Or your script needs to check if the component is attached before using it. CharaMove.Update () (at Assets/Scripts/CharaMove.cs:41)
がエラーの内容です。
c#
1 2using System.Collections; 3using System.Collections.Generic; 4using UnityEngine; 5 6public class CharaMove : MonoBehaviour 7{ 8 9 //移動速さ、落ちる速さ、ジャンプする速さ 10 public float speed = 3f; 11 public float gravity = 10f; 12 public float jumpSpeed = 5f; 13 14 //Playerの移動や向く方向を入れる 15 Vector3 moveDirection; 16 17 //CharacterControllerを変数にする 18 CharacterController controller; 19 20 //マウスの横縦に動かす速さ 21 public float horizontalSpeed = 2.0F; 22 public float verticalSpeed = 2.0F; 23 24 //Main Cameraを入れる 25 GameObject camera; 26 27 void Start() 28 { 29 30 //CharacterControllerを取得 31 controller = GetComponent<CharacterController>(); 32 33 //Main Cameraを検索し子オブジェクトにしてPlayerに設置する 34 camera = GameObject.Find("Main Camera"); 35 camera.transform.parent = this.transform; 36 camera.transform.localPosition = new Vector3(0, 0.4f, 0); 37 } 38 39 void Update() 40 { 41 //Playerが地面に設置していることを判定 42 if (controller.isGrounded) 43 { 44 //マウスでカメラの向きとPlayerの横の向きを変える 45 float h = horizontalSpeed * Input.GetAxis("Mouse X"); 46 float v = verticalSpeed * Input.GetAxis("Mouse Y"); 47 transform.Rotate(0, h, 0); 48 camera.transform.Rotate(v, 0, 0); 49 50 //XZ軸の移動と向きを代入する 51 moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, 52 Input.GetAxis("Vertical")); 53 moveDirection = transform.TransformDirection(moveDirection); 54 moveDirection *= speed; 55 56 // Y軸方向にジャンプさせる 57 if (Input.GetButtonDown("Jump")) 58 moveDirection.y = jumpSpeed; 59 } 60 61 // 重力を設定しないと落下しない 62 moveDirection.y -= gravity * Time.deltaTime; 63 64 // Move関数に代入する 65 controller.Move(moveDirection * Time.deltaTime); 66 67 } 68}
あなたの回答
tips
プレビュー