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