- Home /
Question by
DRRosen3 · Nov 27, 2014 at 03:57 PM ·
cameracamera-movementplayer movement
Player Movement / Camera Tracking Issue
The problem I'm having is that while the player is in motion the camera is jerking back and forth between the player.transform.position and where it's SUPPOSED to be.
Here is the Camera script...
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour {
public Transform cameraTarget;
private float x = 0.0f;
private float y = 0.0f;
private int mouseXSpeedMod = 5;
private int mouseYSpeedMod = 3;
public float maxViewDistance = 25;
public float minViewDistance = 1;
public int zoomRate = 30;
private int lerpRate = 2;
private float distance = 3;
private float desiredDistance;
private float correctedDistance;
private float currentDistance;
public float cameraTargetHeight = 1.5f; //This number is the height of the camera's target. i.e. The player.
void Start () {
cameraTarget = GameObject.FindGameObjectWithTag("Player").transform;
Vector3 angles = transform.eulerAngles;
x = angles.x;
y = angles.y;
currentDistance = distance;
desiredDistance = distance;
correctedDistance = distance;
}
void Update(){
}
void LateUpdate () {
if(Input.GetMouseButton (1)){
x += Input.GetAxis("Mouse X") * mouseXSpeedMod;
y -= Input.GetAxis("Mouse Y") * mouseYSpeedMod;
}
else if(Input.GetAxis("Movement") != 0 || Input.GetAxis("Rotate Player") != 0){
float targetRotationAngle = cameraTarget.eulerAngles.y;
float cameraRotationAngle = transform.eulerAngles.y;
x = Mathf.LerpAngle(cameraRotationAngle, targetRotationAngle, lerpRate * Time.deltaTime);
}
y = ClampAngle(y, -50, 80);
Quaternion rotation = Quaternion.Euler(y, x, 0);
desiredDistance -= Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * zoomRate * Mathf.Abs (desiredDistance);
desiredDistance = Mathf.Clamp(desiredDistance, minViewDistance, maxViewDistance);
correctedDistance = desiredDistance;
Vector3 position = cameraTarget.position - (rotation * Vector3.forward * desiredDistance);
RaycastHit collisionHit;
Vector3 cameraTargetPosition = new Vector3(cameraTarget.position.x, cameraTarget.position.y + cameraTargetHeight,cameraTarget.position.z);
bool isCorrected = false;
if(Physics.Linecast(cameraTargetPosition, position, out collisionHit)){
position = collisionHit.point;
correctedDistance = Vector3.Distance(cameraTargetPosition, position);
isCorrected = true;
}
currentDistance = !isCorrected || correctedDistance > currentDistance ? Mathf.Lerp(currentDistance, correctedDistance, Time.deltaTime * zoomRate) : correctedDistance;
position = cameraTarget.position - (rotation * Vector3.forward * currentDistance + new Vector3(0, -cameraTargetHeight, 0));
transform.rotation = rotation;
transform.position = position;
}
private static float ClampAngle(float angle, float min, float max){
if(angle < -360){
angle += 360;
}
if(angle > 360){
angle -= 360;
}
return Mathf.Clamp(angle, min, max);
}
}
And here is the Player Movement script...
using UnityEngine;
using System.Collections;
[RequireComponent (typeof(CharacterController))]
public class Movement : MonoBehaviour {
public float rotateSpeed = 250f;
public float moveSpeed = 5f;
private CharacterController controller;
private Transform myTransform;
private Animator anim;
void Awake(){
controller = GetComponent<CharacterController>();
myTransform = transform;
}
void Start () {
anim = GetComponent<Animator>();
}
void Update () {
Turn();
Walk();
}
private void Turn(){
if(Mathf.Abs(Input.GetAxis("Rotate Player")) > 0){
myTransform.Rotate(0, Input.GetAxis("Rotate Player") * Time.deltaTime * rotateSpeed, 0);
anim.SetFloat("Direction", Input.GetAxis("Rotate Player"));
}else{
anim.SetFloat("Direction", 0);
}
}
private void Walk(){
if(Mathf.Abs(Input.GetAxis("Movement")) > 0){
controller.SimpleMove(myTransform.TransformDirection(Vector3.forward) * Input.GetAxis("Movement") * moveSpeed);
anim.SetFloat("Speed", Input.GetAxis("Movement"));
}else{
anim.SetFloat("Speed", 0);
}
}
}
In case it matters... I have a game object called GameMaster which instantiates the player at the start of the scene. Then it instantiates the camera.
Comment
Alright, so growing impatient for responses I went through my $$anonymous$$ovement script piece by piece (commenting out lines and re-adding them little-by-little). The camera jerking back and forth doesn't start happening until I add in the anim.SetFloat("Speed", Input.GetAxis("$$anonymous$$ovement"))...any thoughts?