- Home /
Why is the camera going crazy when the player moves?
Everything works perfectly..that is untill the player starts moving..when he does, the camera starts rotating around the player in every angle like crazy. Why is that happening? I want the rotation to stay exactly as it was until the user rotates the camera once again. BUT if i try to remove transform.rotation = rotation when the player starts moving then the camera starts trembling. Please help me out guys... Thanks.
using UnityEngine; using System.Collections;
public class CamaraJugador : MonoBehaviour { public Transform CameraTarget; private float x = 0.0f; private float y = 0.0f;
private int mouseXSpeedMod = 5;
private int mouseYSpeedMod = 5;
public float MaxViewDistance = 15f;
public float MinViewDistance = 1f;
public int ZoomRate = 20;
private int lerpRate = 5;
private float distance = 3f;
private float desireDistance;
private float correctedDistance;
private float currentDistance;
public bool TargetMoving;
public Vector3 TargetOffset;
public static float CamRot;
public float LockedRot_x;
public float LockedRot_y;
public float LockedRot_z;
public float cameraTargetHeight = 1.0f;
//checks if first person mode is on
private bool click = false;
//stores cameras distance from player
private float curDist = 0;
// Use this for initialization
void Start () {
Vector3 Angles = transform.eulerAngles;
x = Angles.x;
y = Angles.y;
currentDistance = distance;
desireDistance = distance;
correctedDistance = distance;
CamRot = transform.eulerAngles.y;
}
// Update is called once per frame
void LateUpdate () {
CamRot = transform.eulerAngles.y;
TargetMoving = Movement.Moving;
if (Input.GetMouseButton (1)) {/*0 mouse btn izq, 1 mouse btn der*/
x += Input.GetAxis("Mouse X") * mouseXSpeedMod;
y -= Input.GetAxis("Mouse Y") * mouseYSpeedMod;
}else if (Input.GetAxis("Vertical") != 0 || Input.GetAxis("Horizontal") != 0)
{
float targetRotantionAngle = CameraTarget.eulerAngles.y;
float cameraRotationAngle = transform.eulerAngles.y;
x = Mathf.LerpAngle(cameraRotationAngle,targetRotantionAngle, lerpRate * Time.deltaTime);
x = x - 180;
}
y = ClampAngle (y, -15, 25);
Quaternion rotation = Quaternion.Euler (y,x,0);
/*if (Input.GetMouseButtonUp (1))
{
LockedRot_x = transform.eulerAngles.x;
LockedRot_y = transform.eulerAngles.y;
LockedRot_z = transform.eulerAngles.z;
Debug.Log(LockedRot_x);
Debug.Log(LockedRot_y);
Debug.Log(LockedRot_z);
}*/
desireDistance -= Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * ZoomRate * Mathf.Abs(desireDistance);
desireDistance = Mathf.Clamp (desireDistance, MinViewDistance, MaxViewDistance);
correctedDistance = desireDistance;
Vector3 position = CameraTarget.position - (rotation * Vector3.forward * desireDistance);
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;
}
//?
//condicion ? first_expresion : second_expresion;
//(input > 0) ? isPositive : isNegative;
currentDistance = !isCorrected || correctedDistance > currentDistance ? Mathf.Lerp(currentDistance,correctedDistance,Time.deltaTime * ZoomRate) : correctedDistance;
position = CameraTarget.position - (rotation * Vector3.forward * currentDistance + new Vector3 (0, -cameraTargetHeight, 0));
if (TargetMoving == false)
{
transform.rotation = rotation;
transform.position = position;
} else
{
if (Input.GetMouseButton (1))
{
transform.rotation = rotation;
transform.position = position;
} else
{
transform.rotation = rotation;
transform.position = position;
}
}
//CameraTarget.rotation = rotation;
float cameraX = transform.rotation.x;
//checks if right mouse button is pushed
if(Input.GetMouseButton(1))
{
//sets CHARACTERS x rotation to match cameras x rotation
//CameraTarget.eulerAngles = new Vector3(cameraX,transform.eulerAngles.y,transform.eulerAngles.z);
}
//checks if middle mouse button is pushed down
if(Input.GetMouseButtonDown(2))
{
//if middle mouse button is pressed 1st time set click to true and camera in front of player and save cameras position before mmb.
//if mmb is pressed again set camera back to it's position before we clicked mmb 1st time and set click to false
if(click == false)
{
click = true;
curDist = distance;
distance = distance - distance - 1;
}
else
{
distance = curDist;
click = false;
}
}
}
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);
}
}
Your answer
Follow this Question
Related Questions
How to move camera up and down 1 Answer
RTS Camera movement wrong after rotation 1 Answer
Smooth movement between two exact locations 0 Answers
2D Aiming in a 3D game 0 Answers
Flip over an object (smooth transition) 3 Answers