I created a cube (named Player). I attached it a 3rd Person CharacterController by importing the CharecterController Asset and drag-and-dropping the 3rd person CC icon from project-> StandardAsset to the Hierarchy->Player. In the script I have the line:
this.GetComponent ().Move (displacement);
But after change to Game and press Play, in the console I have the message:
MissingComponentException: There is no ‘CharacterController’ attached to the “Player1” game object, but a script is trying to access it.
I don’t know how to solve it. Please, could you help me ?
Click your cube called “Player1” in the scene. Make sure it’s not just his parent/child object. Now when the game runs, check to see if the script is enabled. If it is not, you may have a script that disables it at runtime.
Thanks. In the script I have a public variable (moveSpeed) that is set to 100.0f.
When I press play in Game, this variable drops down to zero, but in scene is correct, i.e. 100. I guess this is the cause, but I don’t know how to set it back to 100. In fact I have two scripts. First is GameCam.cs and is attached to MainCamera:
using UnityEngine;
using System.Collections;
public class GameCam : MonoBehaviour {
public GameObject trackObj;
public float height;
public float desiredDistance;
public float heightDamp;
public float rotDamp;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
UpdateRotAndTrans ();
}
void UpdateRotAndTrans(){
float DesiredRotationAngle;
float DesiredHeight;
if (trackObj) {
DesiredRotationAngle = trackObj.transform.eulerAngles.y;
DesiredHeight = trackObj.transform.position.y + height;
float RotAngle = transform.eulerAngles.y;
float Height = transform.position.y;
RotAngle = Mathf.LerpAngle(RotAngle, DesiredRotationAngle, rotDamp);
Height = Mathf.Lerp(Height, DesiredHeight, heightDamp*Time.deltaTime);
Quaternion CurrentRotation = Quaternion.Euler(0.0f, RotAngle, 0.0f);
Vector3 pos = trackObj.transform.position;
pos -= CurrentRotation * Vector3.forward * desiredDistance;
pos.y = Height;
transform.position = pos;
transform.LookAt(trackObj.transform.position);
} else {
Debug.Log ("Game Camera: Error, trackObj invalid !");
}
}
}
The second (where is the line I mentioned) is PlayerControls.cs:
using UnityEngine;
using System.Collections;
public class PlayerControls : MonoBehaviour {
Vector3 moveDirection = Vector3.zero;
public float rotateSpeed;
public float moveSpeed = 100.0f;
public float speedSmoothing = 10.0f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
UpdateMovement ();
}
void UpdateMovement(){
Vector3 cameraForward = Camera.mainCamera.transform.TransformDirection (Vector3.forward);
cameraForward.y = 0.0f;
cameraForward.Normalize ();
Vector3 cameraRight = new Vector3 (cameraForward.z, 0.0f, -cameraForward.x);
float v = Input.GetAxisRaw ("Vertical");
float h = Input.GetAxisRaw ("Horizontal");
Vector3 targetDirection = h * cameraRight + v * cameraForward;
moveDirection = Vector3.RotateTowards (moveDirection, targetDirection, rotateSpeed * Mathf.Deg2Rad * Time.deltaTime, 1000);
moveDirection = moveDirection.normalized;
float curSmooth = speedSmoothing * Time.deltaTime;
float targetSpeed = Mathf.Min (targetDirection.magnitude, 1.0f);
moveSpeed = Mathf.Lerp (moveSpeed, targetSpeed, curSmooth);
Vector3 displacement = moveDirection * moveSpeed * Time.deltaTime;
this.GetComponent<CharacterController> ().Move (displacement);
transform.rotation = Quaternion.LookRotation (moveDirection);
}
}