Spaceship rotation by mouse | Problem Solved!

Hi,

I’m trying to convert the MouseLook.cs Y rotation into a Z axis rotation. So that you can look up and down, but not left/right because it should swifel along the z axis.
I’ve tried it down here, but it just uses the same Y axis.

The forward/-forward is handled by the keyboard.

using UnityEngine;
using System.Collections;

/// Mouserotates the transform based on the mouse delta.
/// Minimum and Maximum values can be used to constrain the possible rotation

/// To make an FPS style character:
/// - Create a capsule.
/// - Add a rigid body to the capsule
/// - Add the MouseLook script to the capsule.
///   -> Set the mouse look to use LookX. (You want to only turn character but not tilt it)
/// - Add FPSWalker script to the capsule

/// - Create a camera. Make the camera a child of the capsule. Reset it's transform.
/// - Add a MouseLook script to the camera.
///   -> Set the mouse look to use LookY. (You want the camera to tilt up and down like a head. The character already turns.)
[AddComponentMenu("Camera-Control/Mouse Look")]
public class MouseMove : MonoBehaviour {

	public enum RotationAxes { MouseXAndZ = 0, MouseX = 1, MouseZ = 2 }
	public RotationAxes axes = RotationAxes.MouseXAndZ;
	public float sensitivityX = 15F;
	public float sensitivityZ = 15F;

	public float minimumX = -360F;
	public float maximumX = 360F;

	public float minimumZ = -60F;
	public float maximumZ = 60F;

	float rotationX = 0F;
	float rotationZ = 0F;
	
	Quaternion originalRotation;

	void Update ()
	{
		if (axes == RotationAxes.MouseXAndZ)
		{
			// Read the mouse input axis
			rotationX += Input.GetAxis("Mouse X") * sensitivityX;
			rotationZ += Input.GetAxis("Mouse Y") * sensitivityZ;

			rotationX = ClampAngle (rotationX, minimumX, maximumX);
			rotationZ = ClampAngle (rotationZ, minimumZ, maximumZ);
			
			Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
			Quaternion zQuaternion = Quaternion.AngleAxis (rotationZ, Vector3.left);
			
			transform.localRotation = originalRotation * xQuaternion * zQuaternion;
		}
		else if (axes == RotationAxes.MouseX)
		{
			rotationX += Input.GetAxis("Mouse X") * sensitivityX;
			rotationX = ClampAngle (rotationX, minimumX, maximumX);

			Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
			transform.localRotation = originalRotation * xQuaternion;
		}
		else
		{
			rotationZ += Input.GetAxis("Mouse Y") * sensitivityZ;
			rotationZ = ClampAngle (rotationZ, minimumZ, maximumZ);

			Quaternion zQuaternion = Quaternion.AngleAxis (rotationZ, Vector3.left);
			transform.localRotation = originalRotation * zQuaternion;
		}
	}
	
	void Start ()
	{
		// Make the rigid body not change rotation
		if (rigidbody)
			rigidbody.freezeRotation = true;
		originalRotation = transform.localRotation;
	}
	
	public static float ClampAngle (float angle, float min, float max)
	{
		if (angle < -360F)
			angle += 360F;
		if (angle > 360F)
			angle -= 360F;
		return Mathf.Clamp (angle, min, max);
	}
}

One thing that springs to mind is that Vector3.left isn’t documented and isn’t really supposed to be used (another recent problem may have been due to this). Try using Vector3.right as the rotation axis and see if it makes any difference here.

Okay,

Now I have my mouse rotation script in Javascript, which is a lot more understandable for me.

When you roll over a few degrees and then look up, it doesn’t look up at the angle it’s rolled. It just looks up from the “world” perspective.

How can I make it look up according to the angle it’s rolled?

Here’s the script, I have put the movement in here as well.

var initialRot : Quaternion; 

var forwardSpeed= 20; 
var riseSpeed= 10;
var boostSpeed= 40;


function Start () {    
    initialRot = transform.rotation; 
} 

function Update() { 

   Debug.Log(Input.GetAxis ("Mouse X")*-5); 
   if(Input.GetAxis("Mouse X") != 0) { 
		transform.rotation.eulerAngles.z +=  Input.GetAxis ("Mouse X")*-5; 
   } 
   
	Debug.Log(Input.GetAxis ("Mouse Y")*-5); 
	if(Input.GetAxis("Mouse Y") != 0) { 
		transform.rotation.eulerAngles.x +=  Input.GetAxis ("Mouse Y")*-5;
	} 

   
   
   
   
   
if (Input.GetKey (KeyCode.W)) {
transform.position += transform.forward * forwardSpeed * Time.deltaTime;
}

if (Input.GetKey (KeyCode.S)) {
transform.position -= transform.forward * riseSpeed * Time.deltaTime;
}
   
   
   
   
   
}

Don’t monkey with the angles directly.

use transform.Rotate():

Rotate (eulerAngles : Vector3, relativeTo : Space = Space.Self)

or better yet apply acceleration in the FixedUpdate():

		rigidbody.AddRelativeTorque(0, a1.leftX*15, 0, ForceMode.Acceleration);

Ahaa, that sounds right. To have the rotation for the object and not the world.
Using Torque instead of normal go forward with 3 speed is what I want.

But how do I add the Space.Self script? I’ve tried a few things but then unity won’t accept the : and =.

This is how I have my flight controls now.
I’d like to add Torque for speed and making it smoother and more realistic.
And the Space.Self doesn’t seem to work.

Any help is welcome

var initialRot : Quaternion; 

var forwardSpeed= 20; 
var riseSpeed= 10;
var boostSpeed= 40;


function Start () {    
    initialRot = transform.rotation; 
} 

function Update() { 

   Debug.Log(Input.GetAxis ("Mouse X")*-5); 
   if(Input.GetAxis("Mouse X") != 0) { 
		transform.Rotate(0, 0 , Input.GetAxis ("Mouse X")*-5, Space.Self);
   } 
   
	Debug.Log(Input.GetAxis ("Mouse Y")*-5); 
	if(Input.GetAxis("Mouse Y") != 0) { 
		transform.Rotate (Input.GetAxis ("Mouse Y")*-5, 0 , 0);
	} 

   
   
   
   
   
if (Input.GetKey (KeyCode.W)) {
transform.position += transform.forward * forwardSpeed * Time.deltaTime;
}

if (Input.GetKey (KeyCode.S)) {
transform.position -= transform.forward * riseSpeed * Time.deltaTime;
}
   
   
   
   
   
}

Yoh!

Problem solved, I allready had the right answer in correct code, just needed to be copied to the other mouse input.

What was the complete finished code you used for your spaceship controls? I’d be intrested to know because I’m trying to do something similar.