Hello, I’ve been following a tutorial all the steps and I think I missed something cus I’m getting a null reference error and my camera aint following my Character. It’s a 3rd person character Im trying to create
using UnityEngine;
using System.Collections;
public class TP_Controller : MonoBehaviour
{
public static CharacterController Charactercontroller;
public static TP_Controller Instance;
void Awake ()
{
Charactercontroller = GetComponent ("CharacterController") as CharacterController;
Instance = this;
TP_Camera.UseExistingOrCreateNewMainCamera ();
}
void Update ()
{
if (Camera.mainCamera == null)
{
return;
}
GetLocoMotionInput ();
TP_Motor.Instance.UpdateMotor ();
}
void GetLocoMotionInput()
{
var deadzone = 0.1f;
TP_Motor.Instance.MoveVector = Vector3.zero;
if(Input.GetAxis("Vertical") > deadzone || Input.GetAxis("Vertical") < -deadzone)
TP_Motor.Instance.MoveVector += new Vector3(0, 0, Input.GetAxis("Vertical"));
if(Input.GetAxis("Horizontal") > deadzone || Input.GetAxis("Horizontal") < -deadzone)
TP_Motor.Instance.MoveVector += new Vector3(Input.GetAxis("Horizontal"), 0, 0);
}
}
I tried going back to the tutorials and I can’t find it, the problem I got now is that the camera aint following the character and if anyone could help me resolve that problem. It said I got an null reference, instance not set to an object, something like that at this line
TP_Motor.Instance.MoveVector = Vector3.zero;
I would be greatfull if anyone could help me.
EDIT 1:
Here’s the TP_Camera script:
using UnityEngine;
using System.Collections;
public class TP_Camera : MonoBehaviour
{
public static TP_Camera Instance;
public Transform TargetLookAt;
public float Distance = 5f;
public float DistanceMin = 3f;
public float DistanceMax = 10f;
public float X_mouseSensitivity = 5f;
public float Y_mouseSensitivity = 5f;
public float mouseWheelSensitivity = 5f;
public float Y_MinLimit = -40f;
public float Y_MaxLimit = 80f;
public float distanceSmooth = 0.05f;
private float velDistance = 0f;
public float X_Smooth = 0.05f;
public float Y_Smooth = 0.1f;
private float MouseX = 0f;
private float velX = 0f;
private float velY = 0f;
private float velZ = 0f;
private float MouseY = 0f;
private float StartDistance = 0f;
private Vector3 desiredPosistion = Vector3.zero;
private float DesiredDistance = 0f;
private Vector3 Posistion = Vector3.zero;
void Awake()
{
Instance = this;
}
void Start()
{
Distance = Mathf.Clamp (Distance, DistanceMin, DistanceMax);
StartDistance = Distance;
Reset ();
}
void LateUpdate()
{
if (TargetLookAt == null)
return;
HandlePlayerInput ();
CalculateDesiredPosistion ();
UpdatePosistion ();
}
void HandlePlayerInput()
{
var deadZone = 0.01f;
if (Input.GetMouseButton (1))
{
MouseX += Input.GetAxis("Mouse X") * X_mouseSensitivity;
MouseY -= Input.GetAxis("Mouse Y") * Y_mouseSensitivity;
}
MouseY = Helper.ClampAngle (MouseY, Y_MinLimit, Y_MaxLimit);
if (Input.GetAxis ("Mouse ScrollWheel") < -deadZone || Input.GetAxis ("Mouse ScrollWheel") > deadZone)
{
DesiredDistance = Mathf.Clamp(Distance - Input.GetAxis ("Mouse ScrollWheel") * mouseWheelSensitivity, DistanceMin, DistanceMax);
}
}
public void Reset()
{
MouseX = 0;
MouseY = 10;
Distance = StartDistance;
DesiredDistance = Distance;
}
void CalculateDesiredPosistion()
{
Distance = Mathf.SmoothDamp (Distance, DesiredDistance, ref velDistance, distanceSmooth);
desiredPosistion = CalculatePosistion (MouseY, MouseX, Distance);
}
Vector3 CalculatePosistion(float rotationX, float rotationY, float distance )
{
Vector3 direction = new Vector3 (0, 0, -distance);
Quaternion rotation = Quaternion.Euler (rotationX, rotationY, 0);
return TargetLookAt.position + rotation * direction;
}
void UpdatePosistion()
{
var posX = Mathf.SmoothDamp (Posistion.x, desiredPosistion.x, ref velX, X_Smooth);
var posY = Mathf.SmoothDamp (Posistion.y, desiredPosistion.y, ref velY, Y_Smooth);
var posZ = Mathf.SmoothDamp (Posistion.z, desiredPosistion.z, ref velZ, X_Smooth);
Posistion = new Vector3 (posX, posY, posZ);
transform.position = Posistion;
transform.LookAt (TargetLookAt);
}
public static void UseExistingOrCreateNewMainCamera()
{
GameObject tempCamera;
GameObject targetLookAt;
TP_Camera myCamera;
if (Camera.mainCamera != null)
{
tempCamera = Camera.mainCamera.gameObject;
}
else
{
tempCamera = new GameObject("Main Camera");
tempCamera.AddComponent("Camera");
tempCamera.tag = "MainCamera";
}
tempCamera.AddComponent ("TP_Camera");
myCamera = tempCamera.GetComponent ("TP_Camera") as TP_Camera;
targetLookAt = GameObject.Find ("TargetLookAt") as GameObject;
if (targetLookAt == null)
{
targetLookAt = new GameObject("targetLookAt");
targetLookAt.transform.position = Vector3.zero;
}
myCamera.TargetLookAt = targetLookAt.transform;
}
}