- Home /
Camera Control Help
I have this PlayerCameraControl script that controls the camera when you move the character and when you press and hold the right mouse button. When you press the right mouse button, you than can rotate the camera around the character through the mouse. The problem I'm having is that when you press the right mouse button, the camera snaps to the same place every time. I want the camera to always start behind the player no matter the direction. So if my character is facing down the negative x, I want my camera's starting position when I press it to be right behind him. Can someone help me out.
//Camera That is used for player var mainCameraTransform : Transform; // The target we are following var target : Transform; // The distance in the x-z plane to the target var distance = 10.0; // the height we want the camera to be above the target var height = 5.0; // How much we var heightDamping = 2.0; var rotationDamping = 3.0;
//place holder for x and y postion of mouse var x : float; var y : float;
//The rotation speed of the x and y for the manual camera rotationDamping var xSpeed : float = 250.0; var ySpeed : float = 120.0;
//boolean for mouse click var buttonPressed: boolean = false;
function Start(){
if(mainCameraTransform == null)
mainCameraTransform = Camera.main.transform;
target = transform;
}
function Update(){
if(Input.GetMouseButtonDown(1)){
buttonPressed = true;
}
if(Input.GetMouseButtonUp(1)){
buttonPressed = false;
}
}
function LateUpdate () { // Early out if we don't have a target if (!target) return;
if(buttonPressed){
var startDirection = mainCameraTransform.position - target.position;
x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
var tempRotation = Quaternion.Euler(y, x, 0);
//var tempPosition = tempRotation * Vector3(0.0,height,distance) + target.position;
var tempPosition = tempRotation * Vector3(0.0,0.0,distance) + target.transform.position;
mainCameraTransform.transform.rotation = tempRotation;
mainCameraTransform.transform.position = tempPosition;
mainCameraTransform.transform.LookAt(target);
}
else{
x = 0;
y = 0;
// Calculate the current rotation angles
wantedRotationAngle = target.eulerAngles.y;
wantedHeight = target.position.y + height;
currentRotationAngle = mainCameraTransform.transform.eulerAngles.y;
currentHeight = mainCameraTransform.transform.position.y;
// Damp the rotation around the y-axis
currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
// Damp the height
currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);
// Convert the angle into a rotation
currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);
// Set the position of the camera on the x-z plane to:
// distance meters behind the target
mainCameraTransform.transform.position = target.position;
mainCameraTransform.transform.position -= currentRotation * Vector3.forward * distance;
// Set the height of the camera
mainCameraTransform.transform.position.y = currentHeight;
// Always look at the target
mainCameraTransform.transform.LookAt (target);
}
}
Your answer
Follow this Question
Related Questions
How to make camera position relative to a specific target. 1 Answer
how to find mouse look 1 Answer
Camera.main.ScreenToWorldPoint works within Unity editor, but not in builds 0 Answers
Age of empires Mouse and Camera controls (SOLVED) 2 Answers
Freeze Camera Rotation that is attached to the object in certain moments 0 Answers