- Home /
Cant seem to make this basic 3D car turn properly
So basically im a car on the road, got 3 lanes, it spawns in the middle, it has a rigidbody so I make it go foward with:
rb.AddRelativeForce(1000 * 4, 0, 0);
Then I want to add the following feature: When the user presses the right key, it moves to the right lane, and if the left key is pressed it moves to the left lane, nothing to complicated but I just cant seem to get the hang of it. I can make it work using transform.position obviously but it just 'teleports' there, I want it to have that cartoon arcady feeling, it turns, moves then re-adjusts itself to face forward. I just cant seem to make it work, i've used transform.translate, LookAt, FromToRotation, etc... Just cant seem to find out how to make it work.
The best solution I have right now is the following:
public class CarMove : MonoBehaviour {
public Rigidbody rb;
public Transform car;
private int direcha;
private float carZ;
private Quaternion deltaQuat;
private Vector3 axis;
private float angle;
private void Update() {
if (Input.GetKeyDown("left")) {
carZ = car.position.z - 4;
direcha = 1;
}
if (Input.GetKeyDown("right")) {
carZ = car.position.z + 4;
direcha = 2;
}
}
void FixedUpdate() {
if (direcha == 1) {
rb.AddRelativeTorque(0,1f,0);
StartCoroutine(Carro());
deltaQuat = Quaternion.FromToRotation(new Vector3(car.position.x + 4,0,0), Vector3.right);
deltaQuat.ToAngleAxis(out angle, out axis);//I add 4 on the line above to make it change lanes
} else if (direcha == 2) {
rb.AddRelativeTorque(0, -1f,0);
StartCoroutine(Carro());
deltaQuat = Quaternion.FromToRotation(new Vector3(car.position.x - 4,0,0), Vector3.right);
deltaQuat.ToAngleAxis(out angle, out axis);//I subtract 4 on the line above to make it change lanes
} else {
deltaQuat = Quaternion.FromToRotation(new Vector3(car.position.x,0,0), Vector3.right);
deltaQuat.ToAngleAxis(out angle, out axis);
}
rb.AddRelativeForce(1000 * 4, 0, 0);
float dampenFactor = 0.5f;//This couple lines 'affect' how the angularVelocity acts
rb.AddTorque(-rb.angularVelocity * dampenFactor, ForceMode.Acceleration);
float adjustFactor = 0.9f;//Supposedly this couple lines make the car stabilize itself
rb.AddTorque(axis.normalized * angle * adjustFactor, ForceMode.Acceleration);
}
private IEnumerator Carro() {
yield return new WaitForSeconds(1f);
rb.angularVelocity = new Vector3(0, 0, 0);
rb.AddTorque(axis.normalized * angle * 100, ForceMode.VelocityChange);
direcha = 0;
}
}
The final product is that when I press the right or left arrow keys the car moves correctly with kinda of a nice feeling to it but it then start going forward on the direction it is facing and does not stabilize itself at all... I've been banging my head all day on this and I really dont know how to change it
Your answer
Follow this Question
Related Questions
Animation in a car without affecting the physics? help! 1 Answer
Objects on the stage twitch while moving 0 Answers
The car flies away when wheel collider touch walls or other objects 1 Answer
Need for speed like physics using Edy's vehicle physics? 1 Answer
OnCollisionEnter doesn't work: changing wheelcollider's sideway friction stiffness 0 Answers