- Home /
Player direction in air
How can I make the player car to not be able to turn and change direction in the air?
That's a hard question to answer without more information on how your car is made. If it was a rigidbody you can stop that with
private var grounded = false;
// This part detects whether or not the object is grounded and stores it in a variable
function OnCollisionEnter ()
{
state ++;
if(state > 0)
{
grounded = true;
}
}
function OnCollisionExit ()
{
state --;
if(state < 1)
{
grounded = false;
state = 0;
}
}
function FixedUpdate ()
{
if (grounded == false) {
rigidbody.freezeRotation = true; }}
I have some errors with the script after I added it to it.
var speed : float = 8.0;
var jumpSpeed : float = 9.0;
var turnSpeed : float = 90;
private var moveDirection: Vector3 = Vector3.zero;
private var distGround: float;
private var isGrounded: boolean = false;
private var grounded = false;
function Start(){
rigidbody.freezeRotation = true; // disable physics rotation
var caps: CapsuleCollider = transform.collider;
distGround = caps.height / 2 - caps.center.y; // distance to ground
}
function Update(){
var hit: RaycastHit; // use raycast to check if character grounded
isGrounded = Physics.Raycast(transform.position, -transform.up, distGround + 0.1);
if (isGrounded && Input.GetButtonDown("Jump")){ // only jump from the ground
rigidbody.velocity.y += jumpSpeed;
}
// rotate character and define direction to move
transform.Rotate(0, Input.GetAxis("Horizontal") * turnSpeed * Time.deltaTime, 0);
moveDirection = -Vector3.forward * Input.GetAxis("Vertical") * speed;
}
// This part detects whether or not the object is grounded and stores it in a variable
function OnCollisionEnter ()
{
state ++;
if(state > 0)
{
grounded = true;
}
}
function OnCollisionExit ()
{
state --;
if(state < 1)
{
grounded = false;
state = 0;
}
}
function FixedUpdate(){ // move at FixedUpdate to get stable results
transform.Translate(moveDirection * Time.deltaTime);
}
{
if (grounded == false); {
rigidbody.freezeRotation : true; }}
can you help me?
I have also another problem that says $$anonymous$$ identifier: 'state' how can I fix it?
my bad i forgot a state variable.
add private var state = 0; to your code to fix that error.
That whole part of the code was to store grounded to a variable but looking at your script it seems like you already have one. so take out the extra junk that stores the grounded variable and just change the (grounded to (isGrounded on the freeze rotation command.
it didn't work. and I have a error when I am changing a little bit in the code that says unexpected identifier 'if'
Answer by digiben · Feb 17, 2013 at 06:57 AM
You can shoot a ray from the car down and test collision. If the collision happens and the object (the road or whatever) has a distance greater than the root of the car and the bottom of the car, then you know it's above ground and then you set a bool that is used to not allow turning.. basically disable input until the ray that is being cast every frame gives you a road right under the car.
Your answer
Follow this Question
Related Questions
How do I make my player stop moving when I release the button that I am pressing? 0 Answers
Camera rotating super fast when trying to match the camera and players Y euler angle. 1 Answer
How to make a game objective pointer? 1 Answer
Objects Rotation to modify Objects transform velocity/Direction of travel 1 Answer
How to detect two keys released simultaneously [Input System] 1 Answer