Question by
Goodmanson · May 15, 2016 at 01:30 PM ·
scripting problemcharactercontroller1st person
Problems with Character Controller
I'm making my own 1st person controller script for a game that I'm making because I want to be able to double jump and do other things that the script in the asset they already give you won't allow you to do. The problem is that whenever I run the game I get this error: "NullReferenceException: Object reference not set to an instance of an object NewWalkScript.Update () (at Assets/NewWalkScript.js:34)" And putting a rigidbody on the player just makes everything freak out.
And I can only move my camera side to side. What am I doing wrong?
Thanks!
Here's my code:
#pragma strict
public var movementSpeed : float = 5;
public var mouseSensitivity : float = 5;
public var upDownRange : float = 90;
var verticalRotation : float = 0;
var isFalling = false;
function Start ()
{
Screen.lockCursor = true;
}
function Update () {
//Rotation
var rotLeftRight : float = Input.GetAxis("Mouse X") * mouseSensitivity;
transform.Rotate(0 , rotLeftRight , 0);
verticalRotation = - Input.GetAxis("Mouse Y") * mouseSensitivity;
verticalRotation = Mathf.Clamp(verticalRotation, -upDownRange, upDownRange);
Camera.main.transform.localRotation = Quaternion.Euler(verticalRotation, 0 , 0);
//Movement
var forwardSpeed = Input.GetAxis("Vertical") * movementSpeed;
var sideSpeed = Input.GetAxis("Horizontal") * movementSpeed;
var cc = GetComponent.<CharacterController>();
var speed = new Vector3(sideSpeed, 0, forwardSpeed);
speed = transform.rotation * speed;
cc.SimpleMove(speed);
//Movement
//Jump
if (Input.GetKeyDown(KeyCode.M) && isFalling == false)
{
GetComponent.<Rigidbody>().AddForce(Vector3.up * 200f);
isFalling = true;
if (Input.GetKeyDown(KeyCode.M) && isFalling == true)
{
GetComponent.<Rigidbody>().AddForce(Vector3.up * 200f);
GetComponent.<Rigidbody>().AddForce(Vector3.forward * 100f);
}
}
}
function OnCollisionStay()
{
isFalling = false;
}
//Jump
Comment
Looks like thbe camera you specify isn't found. $$anonymous$$ake sure there is a Camera tagged as $$anonymous$$ain Camera in your scene.
Your answer
![](https://koobas.hobune.stream/wayback/20220612080823im_/https://answers.unity.com/themes/thub/images/avi.jpg)