- Home /
Camera Orbit on Left Click problem
I have this code, and it works for the most part. the only problem is that it only updates its position when i'm turning the character or turning the camera. if i make the character walk forward or backwards, the camera will maintain it's current position until i force it to rotate, then it will snap back to the character. Some help, please?
var target : Transform;
var zoom_distance = 5.0;
var zoom_distanceMin = 5.0;
var zoom_distanceMax = 20.0;
var camera_height = 1.0;
var xSpeed = 250.0;
var ySpeed = 120.0;
var yMinLimit = -75;
var yMaxLimit = 75;
var positionDamping = 2.0;
private var x = 0.0;
private var y = 0.0;
private var zoom = 0.0;
private var last_rotation : Quaternion;
private var doing_smoothfollow : boolean = false;
function Start () {
var angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}
function LateUpdate () {
if (target) {
// Scrollwheel to set camera distance
zoom_distance = Mathf.Clamp(zoom_distance - Input.GetAxis("Mouse ScrollWheel")*2, zoom_distanceMin, zoom_distanceMax);
if(Input.GetButtonDown("Fire2")){
// The Unity script inverses the x and y.
// Also, start adding the mouse rotation to the current transform Euler Angles
x = transform.eulerAngles.y;
y = ClampAngle(transform.eulerAngles.x, yMinLimit, yMaxLimit);
}
if(Input.GetAxis("Fire2")){
// Add mouse values to rotation, based on the previous camera rotation
x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
// Clamp the y so weird things don't happen
y = ClampAngle(y, yMinLimit, yMaxLimit);
var rotation = Quaternion.Euler(y, x, 0);
var position = rotation * Vector3(0.0, camera_height, -zoom_distance) + target.position;
transform.position = position;
transform.rotation = rotation;
last_rotation = rotation;
}
//Smooth follow if turning or tilting or the camera hasn't reached it's last rotation (before it was moved)
else if(Input.GetAxis("Horizontal") || Input.GetAxis("NoseThrust") || doing_smoothfollow){
//TODO: Implement doing_smoothfollow to let camera settle down behind ship
wantedRotationAngle = Quaternion.Angle(transform.rotation, target.rotation);
// Quaternion smooth follow
transform.rotation = Quaternion.Slerp(transform.rotation, target.rotation, positionDamping * Time.deltaTime);
transform.position = transform.rotation * Vector3(0.0, camera_height, -zoom_distance) + target.position;
last_rotation = transform.rotation;
}
else {
//Follow player even when not pressing 3rd mouse button
transform.position = last_rotation * Vector3(0.0, camera_height, -zoom_distance) + target.position;
transform.rotation = last_rotation;
}
}
}
static function ClampAngle (angle : float, min : float, max : float) {
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
// Hacked to get it working with the Euler angles. Could be better I guess...
if(angle > 360+min)
//angle = 180 -angle;
return angle;
if(angle > 180)
angle = -angle;
return Mathf.Clamp (angle, min, max);
}
this looks like the 3D Buzz TPC tutorial , camera controller =]
I would suggest go back and watch all the theory video's in Section 2.
Here's my JS conversion of the camera script :
http://answers.unity3d.com/questions/246709/rotate-around-the-center-of-the-world-with-mouse.html
I'm not sure why your camera isn't snapping to follow the player when player moves. Please clarify :- does the camera snap to behind the character when the character is moving, or only when the character is moving and you move the mouse? By default, when the character is stationary the camera moves freely, when the character moves the camera snaps to behind the player.
Your answer
Follow this Question
Related Questions
Camera Movement issue 1 Answer
Touch mouseorbit 5 Answers
Mouse Orbit with up/down based on target's up/down 0 Answers
A node in a childnode? 1 Answer
Mouse Orbit snapping issues 0 Answers