Hey there, I recently posted about some trouble I was having controlling a car in an endless runner. I have a script, which most is from an online tutorial for a different style of endless runner, but it’s just not working for me.
public class SimplePlayerControl : MonoBehaviour
{
public float PlayerSpeed;
public float turnAngle;
private float PlayerInput;
private float PlayerMove;
void Update()
{
PlayerInput = Input.GetAxis ("Horizontal");
PlayerMove = PlayerInput * PlayerSpeed;
}
void FixedUpdate ()
{
rigidbody.velocity = new Vector3(PlayerMove, -1f, 0.0f);
rigidbody.rotation = Quaternion.Euler (rigidbody.velocity.y, rigidbody.velocity.x * turnAngle, rigidbody.velocity.z);
}
}
That’s the playerController script. My issue is that the car doesn’t react to gravity properly because of this line:
rigidbody.velocity = new Vector3(PlayerMove, -1f, 0.0f);
But I just don’t know what else I can use. All I want to do is move the car from left to right as needed on the X axis, and lock it on the Z axis. Right now, when in mid air, the car moves left to right fine, however when it hits the ground it jiggles in spot and doesn’t want to move left or right very easily. It has a fairly flat, long rectangular box collider for collision so any contact with the ground should be smooth. I’ve also set collision detection to continuous and interpolate is on.
The other issue I’m having is I’m attempting to lock the car on the Z axis so it doesn’t go too far forward or too far back. It works for the most part, however after several jumps the car begins to lag behind and eventually falls off screen. To achieve this I just locked the z-axis position on the rigidbody of the player car. Is there a better way I can do this that will actually work?
Thanks for your help!