- Home /
C# FP_Camera not following Character Controller
Hi everyone, I have been following the 3dbuzz tutorial series on how to make a 3rd person controller. I have this problem with the camera. I've noticed that the camera is not staying with the Character Controller when the character controller moves. I'm not sure what makes the camera stay with the character controller. I tried parenting the camera to the character controller but that didn't change anything. Any help would be appreciated.
TP_Controller
using UnityEngine;
using System.Collections;
public class TP_Controller : MonoBehaviour {
public static CharacterController characterController;
public static TP_Controller instance;
// Use this for initialization
void Awake () {
characterController = GetComponent("CharacterController") as CharacterController;
instance = this;
TP_Camera.UseExistingOrCreateNewMainCamera();
}
// Update is called once per frame
void Update () {
if(Camera.main == 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);
}
}
TP_Motor
using UnityEngine;
using System.Collections;
public class TP_Motor : MonoBehaviour {
public static TP_Motor instance;
public float moveSpeed = 10f;
public Vector3 moveVector {get; set;}
// Use this for initialization
void Awake () {
instance = this;
}
// Update is called once per frame
public void UpdateMotor () {
SnapAlignCharacterWithCamera();
ProcessMotion();
}
void ProcessMotion(){
moveVector = transform.TransformDirection(moveVector);
if(moveVector.magnitude > 1)
moveVector = Vector3.Normalize(moveVector);
moveVector *= moveSpeed;
moveVector *= Time.deltaTime;
TP_Controller.characterController.Move(moveVector);
}
void SnapAlignCharacterWithCamera(){
if(moveVector.x != 0 || moveVector.x != 0){
transform.rotation = Quaternion.Euler(transform.eulerAngles.x, Camera.mainCamera.transform.eulerAngles.y, transform.eulerAngles.z);
}
}
}
TP_Camera
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 distanceSmooth = 0.05f;
public float x_MouseSensitivity = 5f;
public float y_MouseSensitivity = 5f;
public float mouseWheelSensitivity = 5f;
public float x_Smooth = 0.05f;
public float y_Smooth = 0.1f;
public float y_minLimit = -40f;
public float y_maxLimit = 80f;
private float mouseX = 0f;
private float mouseY = 0f;
private float velX = 0f;
private float velY = 0f;
private float velZ = 0f;
private float velDistance = 0f;
private float startDistance = 0f;
private Vector3 position = Vector3.zero;
private Vector3 desiredPosition = Vector3.zero;
private float desiredDistance = 0f;
void Awake(){
instance = this;
}
// Use this for initialization
void Start () {
distance = Mathf.Clamp(distance,distanceMin,distanceMax);
startDistance = distance;
Reset();
}
// Update is called once per frame
void LateUpdate () {
if(TargetLookAt == null)
return;
HandlePlayerInput();
CalculateDesirePosition();
UpdatePosition();
}
void HandlePlayerInput(){
var deadZone = 0.01f;
if(Input.GetMouseButton(1)){
mouseX += Input.GetAxis("Mouse X") * x_MouseSensitivity;
mouseY -= Input.GetAxis("Mouse X") * 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);
}
}
void CalculateDesirePosition(){
distance = Mathf.SmoothDamp(distance, desiredDistance, ref velDistance, distanceSmooth);
desiredPosition = CalculatePosition(mouseY, mouseX, distance);
}
Vector3 CalculatePosition(float rotationX, float rotationY, float dist){
Vector3 direction = new Vector3(0,0, -distance);
Quaternion rotation = Quaternion.Euler(rotationX, rotationY, 0);
return TargetLookAt.position + rotation * direction;
}
void UpdatePosition(){
var posX = Mathf.SmoothDamp(position.x, desiredPosition.x, ref velX, x_Smooth);
var posY = Mathf.SmoothDamp(position.y, desiredPosition.y, ref velY, y_Smooth);
var posZ = Mathf.SmoothDamp(position.z, desiredPosition.x, ref velZ, x_Smooth);
position = new Vector3(posX,posY,posZ);
transform.position = position;
transform.LookAt(TargetLookAt);
}
public void Reset(){
mouseX = 0;
mouseY = 0;
distance = startDistance;
startDistance = distance;
}
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;
}
}
Do you have a script controlling your Camera? Did you child the camera to the character or the other way around? It sounds like there may be something that is setting its position your camera that needs to know about your character. I'm sorry, but I'm not familiar enough with the 3dbuzz tutorials to know what they've instructed you to do.
I child the camera to the character controller. I have some scripts that I made from the tutorials.Would posting those help?
Ok I posted the three scripts I'm using.The first two TP_Controller and TP_$$anonymous$$otor are attached to the character Controller and the third TP_Camera is attached to the camera.
Ok. The code looks good, so I have a couple questions.
Do you have, or does the script create, a GameObject called "targetLookAt"?
If you do, are you sure that the camera isn't following that and that nothings moving it?
When you move the GameObject called targetLookAt, does the camera move?
If none of the above helps, then you should put a Debug.Log() statement in TP_Camera's LateUpdate after the line that says 'return'.
Let me know how it works out.