Hi everyone,
I’m working on a bike game in Unity and need some help with path following. My bike game object is supposed to rotate around a circular path. When I apply a small amount of force, the bike follows the path correctly. However, when I apply more force to increase the bike’s speed, it starts to go off the path.
I want the bike to stay on the path, even as I increase the force and speed. Is there a way to achieve this, so the bike can maintain higher speeds without deviating from the path? Ideally, I want to keep the bike moving dynamically with varying speeds while ensuring it doesn’t leave the circular path
void FixedUpdate()
{
float speedForward = Vector3.Dot(bike.sphereRB.velocity, bike.transform.forward);
float speedLateral = Vector3.Dot(bike.sphereRB.velocity, bike.transform.right);
bike.sphereRB.AddForce(bike.transform.forward * bike.moveInput * bike.maxSpeed);
bike.sphereRB.AddForce(-bike.transform.right * Mathf.Min(Mathf.Abs(speedLateral) * bike.lateralForce, 10f) * Mathf.Sign(speedLateral));
bike.sphereRB.AddForce(-bike.transform.forward * Mathf.Min(Mathf.Abs(speedForward) * bike.sphereRB.mass, 5f) * Mathf.Sign(speedForward));
}
void Update()
{
Vector3 positionDifference = bike.sphereRB.position - lastPos;
float angleRadians = Mathf.Atan2(positionDifference.z, positionDifference.x);
float angleDegrees = angleRadians * Mathf.Rad2Deg;
bike.transform.rotation = Quaternion.Euler(0f, -angleDegrees, 0f);
}
Any advice or tips would be greatly appreciated
At high speeds, the behavior is strange, but it works fine at low speeds.
Thanks