Mouse Orbit to SmoothFollow and back (Euler to Quaternion possible?)

Hello everybody, I have been working on a space ship camera system for the last week and my head is just about to explode. I just can’t do it and I’ve looked everywhere to no avail.

OK, I’ve modified the MouseOrbit script, so it orbits only when the user presses the right mouse button. It saves the last position so when the user clicks again, it doesn’t reset the rotation. Now, when the user presses the “left/right” and “NoseThrust” keys, I’d like it if the camera would “SmoothFollow” the ship. Once the user stops pressing those keys, the camera should stay in that position. So far, I’ve kinda got that somewhat working (not really though).

The huge problem is when the user right-clicks on the screen (to orbit the camera again), there doesn’t seem to be a way to convert my new rotated camera (using eulerAngles) to the weird, cryptic, Quaternion based MouseOrbit that the guys at Unity created. I just don’t get it. I’ve tried many of the camera.ToSomething, or setting localRotation, rotation, rotation.eulerAngles as my last_x and last_y (see script below).

I’ve tried changing the Lerps in SmoothFollow to Quaternion.Lerp and Vector3.Lerp (rotation & position). I’m now experimenting with Quaternion.LookRotation which seems positive, but I get really bad jerking of my ship because the position is nuts (wrong), plus I can’t get the Quaternions to follow the back of the ship… Help please!?

Here’s the MouseOrbitClicked script, it actually works pretty well so hopefully it’ll help somebody one day. The ClampAngle function is available in the Unity MouseOrbit script.

function LateUpdate () {
  if (target) {
    // Scrollwheel to set camera distance
    distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel")*2, distanceMin, distanceMax);
    //Follow player even when not pressing 3rd mouse button
    transform.position = last_rotation * Vector3(0.0, 0.0, -distance) + target.position;
    transform.rotation = last_rotation;

    if(Input.GetButtonDown("Fire2")){
      // Last position = the mouse start point
      x = last_x;
      y = last_y;
    }

    if(Input.GetAxis("Fire2")){
      x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
      y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
      y = ClampAngle(y, yMinLimit, yMaxLimit);
      var rotation = Quaternion.Euler(y, x, 0);
      var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
      transform.position = position;
      transform.rotation = rotation;

      last_rotation = rotation;
      last_x = x;
      last_y = y;
    }
  }
}

Edit:

OK, so I’ve got the smooth follow working using Quaternions (Slerp). So now I’m not using any Euler Angles in my script. Still, the rotation values from the Slerp don’t match up with the ones from my mouse (last_x last_y). Any ideas why ?

Thanks in advance for any tips or help, or just mental support I’m going nuts with this issue :slight_smile:

Finally got it. After studying the original Unity script, I kinda got a better idea of what they were doing with the Quaternions.

Here’s the script, hopefully it helps someone someday. It will let you rotate the camera on right-click, from any point and zoom level it was left off. The ClampAngle function is a hack that works :slight_smile:

var target : Transform;
var zoom_distance = 100.0;

var zoom_distanceMin = 50.0;
var zoom_distanceMax = 500.0;
var camera_height = 10.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);
}

how can I modify the distance using the mouse orbit script, for example in a situation the distance is 10 and when in a smaller space the distance should turn 2, and 10 back after that?