So I have a car and I send a raycast downwards to find the rotation of the ground beneath it, and I rotate the car so that it matches its rotation with the ground.
I also have another function which moves the car based on my horizontal input. How do I make it so that, while the RotationOnHeight function alters the transform.up, I can also rotate the car on Y axis?
void RotationOnHeight()
{
RaycastHit hit;
if (Physics.Raycast(van.position, -van.up, out hit, vanHeight, drivable))
{print ("van close enough");
Debug.DrawLine(van.position, hit.point, Color.red);
van.up = Vector3.Lerp(van.up, hit.normal, Time.deltaTime * rotateDampen);
//van.transform.rotation = Quaternion.LookRotation(rb.velocity, van.up);
van.Rotate(van.up.x, van.eulerAngles.y, van.up.z);
}
else
{
print("No rays");
}
}
void Steer()
{
float absoluteVeloX = Mathf.Abs(rb.velocity.x);
float absoluteVeloZ = Mathf.Abs(rb.velocity.z);
float absoluteVelocity = (absoluteVeloX + absoluteVeloZ) / 2f;
//print("Absolute velocity: " + absoluteVelocity);
horizontal = Input.GetAxis("Horizontal");
switch(horizontal)
{
case -1:
float currentRotation = horizontal;
print("should go left");
rb.AddForce(-van.right * rotateSpeed, ForceMode.Acceleration);
break;
case 1:
print("should go right");
rb.AddForce(van.right * rotateSpeed, ForceMode.Acceleration);
break;
}
}
It almost works with Quaternion.LookRotation, but this time the object turns completely 180 degrees if I’m moving it backwards.