# Object rotation for a motorcycle stunts game

I’m trying to create a rotation for my bikes but I’m struggling with the quaternion construction.

What I need:

1. The bike rotates on its local x axis (back flips, front flips, wheelies).
2. The bike steers on its local y axis.
3. Rolling is not permitted so no rotation on the z axis.
4. Must be a single object, no hierarchy.

I tried creating my quaternion as such:

Quaternion desiredRotation = Quaternion.Euler(flipsRotation, steeringRotation, 0);

The problem is that as I do flips and I’m at 90 degrees on the x axis the bike tries to flip on the z axis.

I tried some work around like this:

float adjustedRoll = 0;
float adjustedYaw = steeringRotation;

//adjust angles if bike is flipped
if(transform.up.y < 0)
{
adjustedYaw = 180 + steeringRotation;
}

This sort of works but I still get some glitches. I’m sure there is a more elegant solution to this problem.

Any ideas?

You can just lock off the angles by setting the rotation variable to zero.

E.g

Var bike : Transform;

Function Update ()

{
If (bike.up.y < 0)
{
AdjustedYaw = 180 + steeringRotation;
bike.rotation.z = 0;
}
}

I can’t guarantee anything though as I’m currently at work and can’t test it.

I fixed my issue!
Here’s how I calculated the final rotation:

Vector3 pitchYawRoll = UtilityFunctions.GetPitchYawRollDeg(transform.rotation);

Quaternion pitchRotation = Quaternion.AngleAxis(pitchYawRoll.x, Vector3.right);
Quaternion yawRotation = Quaternion.LookRotation(steeringDirection, Vector3.up);

Quaternion desiredRotation = yawRotation * pitchRotation;

The code for GetPitchYawRollDeg(Quaternion transform) is in my other question Getting the actual object rotation on just 1 axis - Unity Answers

Lock the z rotation constraint from inspector.

i need help with something , i am trying to check if the player’s rotation is bigger or smaller than a number if it is i will end the game but if i start it it ends immediately
public class PlayerMovement : MonoBehaviour
{
public float sideWaysForce = 0.5f;

protected JoyButton joybutton;

protected JoyButtonRight joybuttonright;

public Rigidbody rb;

public float forward = 2100f;

public float sideWaysForceKEYBOARD = 0.7f;

public AudioSource backgroundMusic;

protected Brake brake;

protected Gas gas;

private float increaseVolume = 0.005f;

private float increasePitch = 0.0005f;

public float maxSpeed = 1600f;

public float minspeed = 0f;

public float minspeedmultiplier = 1.45f;

public Transform playert;

public float delay = 13f;

public void Start()
{
backgroundMusic.Play();
joybutton = FindObjectOfType<JoyButton>();
joybuttonright = FindObjectOfType<JoyButtonRight>();
gas = FindObjectOfType<Gas>();
brake = FindObjectOfType<Brake>();

}
private void Update()
{

backgroundMusic.volume += increaseVolume;
backgroundMusic.pitch += increasePitch;
if (backgroundMusic.pitch > 1)
{
backgroundMusic.pitch = 1f;

}
if (transform.rotation.eulerAngles.x> 60)
{
GameObject.Find("GameManager").SendMessage("Restart");

}
if (transform.rotation.eulerAngles.x > -60)
{
GameObject.Find("GameManager").SendMessage("Restart");

}
}
void FixedUpdate()
{

if (brake.brakeispressed)
{
forward -= 70;
}

if (gas.Gasispressed)
{
forward += 70;
}

if (forward <= 400)
{
forward = forward + 70;

}
if (joybutton.Pressed)
{

rb.AddForce(-sideWaysForce , 0, 0, ForceMode.VelocityChange);
}
if (joybuttonright.PressedRight)
{
rb.AddForce(sideWaysForce , 0, 0, ForceMode.VelocityChange);

}

if (forward > maxSpeed)
{
forward = forward = maxSpeed;

}

rb.AddForce(0, 0,forward * Time.fixedDeltaTime );

if (Input.GetKey("a"))
{
rb.AddForce(-sideWaysForceKEYBOARD, 0, 0, ForceMode.VelocityChange);

}
if (Input.GetKey("s"))
{
forward = forward - 70;

}
if (Input.GetKey("w"))
{
forward = forward + 70;

}
if (Input.GetKey("d"))
{
rb.AddForce(sideWaysForceKEYBOARD, 0, 0, ForceMode.VelocityChange);

}

if (rb.position.y < -1f)
{
restartTimerAndScore();
backgroundMusic.Stop();

}
if (rb.position.y > 3f)
{

restartTimerAndScore();
backgroundMusic.Stop();
}
if (forward < minspeed)
{
forward = forward = minspeed * minspeed;
}

}

public void restartTimerAndScore()
{
GameObject.Find("Player").SendMessage("restartTimer");
GameObject.Find("SC").SendMessage("restartscore");
FindObjectOfType<GameManager>().EndGame();

}

}