- Home /
Gravity Problems on Rigidbody with new Vector3
Hey there, I'm currently making an endless runner style game where you control a car. The car itself never moves on the z-axis, instead the road is sent towards the car. I have the car moving on just the x-axis using most of the script from the Space Shooter tutorial from the Unity website, however I'm having a bit of trouble.
Here is the code,
public class SimplePlayerControl : MonoBehaviour
{
public float speed;
public float turnAngle;
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, 0.0f);
rigidbody.velocity = movement * speed;
rigidbody.rotation = Quaternion.Euler (0.0f, rigidbody.velocity.x * turnAngle, 0.0f);
}
}
Now it's almost working fine except when the car goes over jumps that it encounters, it comes down very slowly. I'm assuming it's because of what I've defined with the new Vector3 (movement).
If I change the code to;
Vector3 movement = new Vector3 (moveHorizontal, -1.0f, 0.0f)
The car slams against the ground and doesn't move on the x-axis very easily. Also when it goes over a jump it descends too quickly. I'd prefer it if it flew threw the air and had a bit of hang-time.
Is there anything I can replace in the code so the car doesn't look to the new Vector3 for gravity, and instead uses the rigidBody mass?
Thanks for your time.
Answer by Baste · Nov 20, 2014 at 08:56 AM
I would suggest that you set only the x-coordinate of your car's velocity, like so:
Vector3 velocity = rigidbody.velocity;
velocity.x = Input.GetAxis(Horizontal);
rigidbody.velocity = velocity;
This allows the gravity to move the car without interference.
If that's not enough, and the falling is still to slow, you'll want to start using raycasts to check if the car's on the ground or not, and add a negative vertical movement when the car's in the air.
Awesome thanks for the tip! I tried adding that in however and now the car barely moves at all and if it does move it moves on the z-axis. Any thoughts?
I really appreciate the time you took to help, by the way!
I've changed the code now, it works much better.
public float xSpeed;
public float turnAngle;
private float xInput;
private float x$$anonymous$$ove;
void Update()
{
xInput = Input.GetAxis ("Horizontal");
x$$anonymous$$ove = xInput * xSpeed;
}
void FixedUpdate ()
{
rigidbody.velocity = new Vector3(x$$anonymous$$ove, -5f, 0.0f);
rigidbody.rotation = Quaternion.Euler (0.0f, rigidbody.velocity.x * turnAngle, 0.0f);
}
I got it from another tutorial for a game similar to what I want to make. $$anonymous$$y problem now is that the car won't rotate when going over jumps, it stays horizontal as it climbs the ramp.
Its because of the rigidbody.rotation line of course, but what values can I use ins$$anonymous$$d of 0.0f to make it so it behaves normally with the ramps? I tried putting in rigidbody.rotation.y and rigidbody.rotation.z but it makes it do the same thing.
I got that line of code from the space shooter tutorial, it's to make the car turn slightly as you move left or right.