Why does my GameObject move to the side when applying force on the Y axis (in world space) and then rotating it (also in world space)?
I'm creating a racing game where the main mechanic is avoiding obstacles along the track by "flipping gravity" and landing on a parallel track. However when I apply force upward and attempt to rotate the vehicle in world space the force pushes the vehicle in an arch sideways like this:
I thought that by applying the force and the rotation in world space would stop this happening. I also tried the rotation using an animation but got very similar results. Obviously, if I don't use a coroutine and simply just set the rotation to (0, 0, 180) and snap to the desired rotation this doesn't happen.
I am fairly new to unity so be gentle :) Can anyone help?
[Header("Flip Gravity")]
public Rigidbody rigidBody;
public Vector3 jump;
public float jumpForce;
public bool flipped;
private void Start()
{
jump = Vector3.up;
}
public void FlipGrav()
{
//if racer rotation is 0 on X
if (flipped == false)
{
Debug.Log("Space pressed");
rigidBody.AddForce(jump * jumpForce, ForceMode.VelocityChange);
StartCoroutine(RotateMe(Vector3.forward * 180, 0.6f));
flipped = true;
}
Your answer
Follow this Question
Related Questions
converting local space vector into world space? 1 Answer
Rotate object by Parent Y and Z, but World X? 0 Answers
Add rotative force in direction of mouse depending on how quick mouse is moved? 0 Answers
Global direction (e.g. Vector3.forward, Vector3.right) from Mouse drag 0 Answers
EdgeCollider2D for the same points as LineRenderer has different position 0 Answers