- Home /
Problem with character controller and collisions
For some reason, after saving a quitting my project then reloading it, my player [complete with CharacterController script] keeps falling through the floor. What happened, how do I fix it and how do I prevent this from happening again?
I know that similar questions have been answered, but I havetn't been able to find one that helps with this problem..
EDIT** I get an error that says "NullReferenceException: Object reference not set to an instance of an object"
using UnityEngine;
using System.Collections;
[RequireComponent (typeof(CharacterController))]
public class FirstPersonController : MonoBehaviour {
public float moveSpeed = 10.0f;
public float mouseSensitivity = 5.0f;
public float jumpSpeed = 7.0f;
public float rotUpDown = 0.0f;
public float headTiltRange = 60.0f;
float vertVelocity = 0;
CharacterController charcont;
// Use this for initialization
void Start () {
Screen.lockCursor = true;
CharacterController charcont = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update () {
//Rotation
float rotLeftRight = Input.GetAxis ("Mouse X") * mouseSensitivity;
transform.Rotate (0, rotLeftRight, 0);
rotUpDown -= Input.GetAxis ("Mouse Y") * mouseSensitivity;
rotUpDown = Mathf.Clamp (rotUpDown, -headTiltRange, headTiltRange);
Camera.main.transform.localRotation = Quaternion.Euler (rotUpDown, 0, 0);
//Movement
float fwdSpeed = Input.GetAxis ("Vertical") * moveSpeed;
float sideSpeed = Input.GetAxis ("Horizontal") * moveSpeed;
//Gravity
vertVelocity += Physics.gravity.y * Time.deltaTime;
Vector3 speed = new Vector3 (sideSpeed, vertVelocity, fwdSpeed);
//Jumping
if (charcont.isGrounded && Input.GetButton ("Jump")) {
//vertVelocity = jumpSpeed;
}
speed = transform.rotation * speed;
charcont.Move (speed * Time.deltaTime);
}
}
Answer by aldonaletto · Sep 02, 2014 at 11:36 PM
CharacterControllers have this bad habit of passing through the terrain: if the CharacterController penetrates even a little into the ground, it falls like a rock. Are you moving the character transform with Translate? Or could the terrain being shifted up by any other part of the code? If none of these, try to place the character at some height above the terrain (0.5 unit, for instance) in the Editor, so that it falls naturally to the ground when the game starts. About the Null Reference error: read the error message to find where it's occurring - maybe this error has nothing to do with the falling character.
The thing is, everything worked 100% before I saved and quit. I moved the character to 20 units above the terrain to be sure it wasn't touching, and it still fell through. Also, I never had that NullReferenceError before I saved and quit as well.
Find where the error is occurring - which script, which line etc. - and post the relevant code
if (charcont.isGrounded && Input.GetButton ("Jump")) { vertVelocity = jumpSpeed; }
Your answer
![](https://koobas.hobune.stream/wayback/20220613162152im_/https://answers.unity.com/themes/thub/images/avi.jpg)