# Rotation-Trouble

I’m a unity-newbie and need some help with my input-script:

When pushing left- and right-buttons, a spaceship shall rotate clockwise- and counterclockwise (z-axis).
Doesn’t sound too difficult but no matter which method I try (transform.rotate, transform.localrotation, rigidbody.moverotation, …) it just won’t rotate. I’ve no problems with the other keys - e.g. moving forward with transform.translate when hitting the tab-key.

Here is one attempt of my many trials. The point of interest is the block “// LEFT-Spin”:

``````    function Update () {

var stepx : float;
var stepz : float;
var minacc : float;
var maxacc : float;

maxacc = 80; //Acelleration
minacc = 10; //Minimum forward-drift

stepx = 8.5; //  Booster Forward-steps
stepz = 1.5; //Sideward steps Slider

// The ship's local z- and x-axis are switched
//  therefore "Vector3.right" is used (instead of "Vector3.forward")

// Nothing pressed
constantForce.relativeForce = (Vector3.right * minacc);

// UP
if(Input.GetKey("w") || Input.GetKey("up")) {
if(constantForce.relativeForce.x <= maxacc){
constantForce.relativeForce = (Vector3.right * maxacc);
}
}
//TABULATOR (Booster)
if(Input.GetKey("tab")) {
transform.Translate(Vector3.right * stepx);  //Booster just moves Obj. forward

}

// DOWN
if(Input.GetKey("s") || Input.GetKey("down")) {

if(constantForce.relativeForce.x >= minacc) {
constantForce.relativeForce = -(Vector3.right * maxacc * 1.15);	 // breaks a bit stronger than accell - Problem: backwards-flying occurs
}
else {

constantForce.relativeForce = (Vector3.right * minacc);
}

}

// LEFT-Slide
if(Input.GetKey("q")) {
transform.Translate(Vector3.forward * stepz);

}

//LEFT-Spin
if(Input.GetKey("left") || Input.GetKey("a")) {

//Syntax:
// transform.Rotate (eulerAngles, relativeTo)
transform.localRotation(Vector3.right*20);

Debug.Log("Object was Rotated");
}

// RIGHT-Slide
if(Input.GetKey("e")) {
transform.Translate(Vector3.forward * -stepz);
}

//RIGHT-Spin
if(Input.GetKey("right") || Input.GetKey("d")) {
transform.RotateAround(Vector3.forward * -20 * Time.deltaTime);
}

}
``````

I applied the script to an object-tree, since my spaceship is made of many parts, including light and camera which all shall move and rotate at the same time, so they behave as if they were one object. Also, there is a second C-script applied reading the mouse-input for X- and Y-axis using quaternions. Could a conflict cause the problem? I studied the half script-reference but none of the alternatives to transform.rotate seem to work.

Thanks for any help in advance.

try
For turning right:
transform.eulerAngles.z += turnspeed * Time.deltaTime;

for turning left:
transform.eulerAngles.z -= turnspeed * Time.deltaTime;

(turnspeed is a variable)

Okay, it seems there is a conflict with the 2nd script “mouselook.cs”. I don’t remember where I got it from. I think it was some standard-script:

``````using UnityEngine;
using System.Collections;

public class MouseLook : MonoBehaviour {

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

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

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

public enum invz { InvertedZ = -1, RegularZ = 1 }    // Z-Inversion? Possible Values: -1 or 1
public invz Inverted = invz.RegularZ;

float rotationX = 0F;
float rotationZ = 0F;
float vz = 1;

Quaternion originalRotation;

void Update ()
{
if (axes == RotationAxes.MouseXAndY)
{
// 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);

if (Inverted == invz.InvertedZ ){
vz = -1;
}
else {
vz = 1;
}

Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
Quaternion zQuaternion = Quaternion.AngleAxis (rotationZ, vz*Vector3.forward);

transform.localRotation = originalRotation * xQuaternion * 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);
}
}
``````

I did remove the else-if-lines for not needed X-only and Y-only-rotation (the script offers a combobox in inspector to choose one of three modes.) The line…

`````` rigidbody.freezeRotation = true;
``````

…seems to disallow rotation. I changed it to false but there is no effect. Since I don’t know C-programming I don’t understand what exactly is happening in the script, why freezing was enabled and why a change don’t take effect. When I disable the script, the transform.rotate in my own javascript-script works.

transform.rotation *= Quaternion.AngleAxis(turnSpeed * Time.deltaTime, transform.up);

or

transform.rotation *= Quaternion.AngleAxis(turnSpeed * Time.deltaTime, Vector3.up);

if you want the rotation to be world relative, rather than relative to itself. If the whole gameplay is 2D, it won’t make a difference, but if you want to add roll (Which would be transform.rotation *= Quaternion.AngleAxis(rollAngle, transform.forward);), that’d feed back and mess you up.

I think I found a solution myself: Java-Script-Mouselook together with a solved possible problem from here. Thanks for the help.