Endless Runner Car Physics Issues (Again)

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!

I’ve discovered physics materials and have solved my issue by changing the script a little bit and giving the player a collider that has no friction. Now my only issue is with rotation.

When I take out this line:

rigidbody.rotation = Quaternion.Euler (rigidbody.velocity.y, rigidbody.velocity.x * turnAngle, rigidbody.velocity.z);

The car moves left and right in the world fine (although it doesn’t stay locked on the z axis for some reason), the car rotates pretty freely when colliding with objects and can sometimes end up facing the wrong way.

Does anyone know of a way to lerp to the correct rotation? I’m assuming I would want it to check if its rotation on the correct axis is 0 degress (facing forward) and if it’s not, then lerp from its current position to the desired position.

Thanks again for anyone’s help.