Facing Forward after crash

Hello,

So im doing just some prototyping and im having problems in finding a way to keep my object facing forward after he crashes, i mean wen he colides with a obstacle the physics engine will make him spin, or tumble, etc, but i wanted it to face forward again after this.

Any simple way to do this?

Player movement code:

public Rigidbody rb;
public float cubeVelocityZ = 1500f;
public float cubeVelocityX = 800f;

// Update is called once per frame
void FixedUpdate ()
{
rb.AddForce(0, 0,cubeVelocityZ * Time.deltaTime);
double halfScreen = Screen.width / 2.0;

if (Input.GetMouseButton (0) & Input.mousePosition.x>halfScreen)
{
rb.AddForce (cubeVelocityX * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (Input.GetMouseButton (0) & Input.mousePosition.x<halfScreen)
{
rb.AddForce (-cubeVelocityX * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}

if (rb.position.y < -1f)
{
FindObjectOfType ().EndGame ();
}
}

Player collision code:

public class PlayerCollision : MonoBehaviour {

public float slowDown =8f;
bool waitDelayTime = false;
public PlayerMovement movement;
public GameManager gameManager;

IEnumerator OnCollisionEnter(Collision collisionInfo)
{
if (collisionInfo.collider.tag == “Obstacle” & waitDelayTime == false)
{
Time.timeScale = 1f / slowDown;
waitDelayTime = true;
Time.fixedDeltaTime = Time.fixedDeltaTime / slowDown;
yield return new WaitForSeconds (0.1f);
Time.timeScale = 1f;
Time.fixedDeltaTime = Time.fixedDeltaTime * slowDown;
waitDelayTime = false;
//FindObjectOfType().EndGame();
}
}

Thanks!!

Can you do something like:

  1. When collision occurs - Freeze player input
  2. Wait for player physics to “settle”
  3. Smoothly reorient body into forward-facing direction
  4. Restore player input

Or are you asking “How do I turn the player around”? This could be achieved in a number of ways, ranging from literally picking up the player Mario Kart style, reorienting them and putting them back down, to having an AI auto-pilot the object into the desired direction, to just teleporting the wrecked player back into forward position. THe common factor though is preventing the player from controlling the wrecked vehicle while changes are made.

1 Like

Well you make an excellent point here, had not think about that, there are a number of ways…my intent was making it face forward but sometimes the player can be upside down if the collision is bigger, so i also have to take care of that, and i wanted it to work kind of in real time so the game keeps its dynamics, i was thinking of doing it while the time is being slowed, i know how to prevent player input, just deactivate the movement script, and i can do it while in slow motion, now just need to know how i reoriente the player :eyes:

You’ll want to calculate the needed changes to all aspects of the character’s orientation. That is, the difference between “upright and forward” and the character’s actual rotation. With that information you can either apply a direct change to make them match immediately (teleporting the character into orientation), or you can apply subtle nudges (perhaps even slowly increasing nudge power) in the direction of correction with addTorque or angularVelocity until the difference between desired orientation and actual orientation is within an acceptable range.

Tip: Quaternions are your friend, despite how complicated they may seem. Here is a simple solution I dug up for using Quaternions to add torque. Will require tweaking to fit your setup.

Thanks for the tips Plystire, still i do think this is maybe too mucth for me who is just starting, i tried but could not make the object face forward in a consistent way, ill leave this project for later, wen i (hopefully) develop more skills :slight_smile:

Well, we’re always here to help look through buggy code when you’re stumped :sunglasses:

A teleport back into forward-facing orientation shouldn’t be a terribly involved solution. With some more information about your project, what you’d like it to do, your code that’s not working, and where you’re getting stuck, we may be able to help clear up any foggy concepts for you :slight_smile: Developing skill can be a group effort! :smile: