- Home /
transform.Translate moving while upside down?
Alright, So I am trying to design a racing type of game, and I have this snippit of code:
if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
{//if "W" or "UpArrow" is pressed
currentSpeed += Speed; //increase the speed
SlowingDownSpeed = 0; //disable slowing down
if(currentSpeed <= 0)
{//if the speed is in reverse / in park
currentSpeed*= -1; //make it positive
}
}
If the car is facing up in air, like this (sorry for bad handwriting): Then If I hold Up Arrow or "W" it goes higher in the air.
The reason I don't think it is normal is if the car is upside down, like this: It still would move forward, I don't know if there is a way to fix this, so any help would be appreciated, thanks!
Answer by Mercbaker · Jul 03, 2017 at 04:50 PM
You probably want to check if the car is on the ground. If the car is on the ground then allow user input.
Lock the user input if the car is in the air.
Also lock the input if the car is on its back. Or you can switch the normal input to affect rotation, so the player input can flip the car back on its wheels.
Once the car is positioned correctly, then allow normal driving input from the Player.
It was on the ground, the collider was at least.
Lock User input, how? Would that just mean put a constraint after it passes a certain y position?
Locking it on it's back I am unsure of, because what if the car is 359 degrees? then it's on it's feet, but flipped?
Your problem is the car is being affected in situations that are not in your games design.
Identify what those situations are, and create a boolean to check for when the player is in that situation.
For example:
If the player is on the ground enable driving input
If the player is NOT on the ground (in the air) disable driving input
If you utilize booleans to check your players state you can better control the players input so that they don't break your game.
So would I just use Rigidbody constraints?
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Unit rotation fails consistently on all slerp rotations after the first? 0 Answers
How do i rotate an object "Once" 1 Answer
accessing an array of rigidbodies at once? c# 2 Answers
Objects not rotating right half the time based on another object 0 Answers