Like i have 2 gameObjects(pair of front wheels,and pair of back wheels) how to keep the same distance between them
One way would be to add a fixed joint between them so they are stuck together
Tho I am not sure if it’s a good approach to make a vehicle
Actually gluing together 2 pairs of wheels won’t do the trick, but you can connect their parents, or even better, make both pairs having same big car parent, through different joints. I guess you would need some kind of rotating one for the front pair and regular fixed to the back pair? Can’t provide a coherent solution but you should definitely look into joint components
Edited Code
public class CarWheels : MonoBehaviour
{
public Rigidbody frontWheel1;
public Rigidbody frontWheel2;
public Rigidbody backWheel1;
public Rigidbody backWheel2;
private float initialDistance;
private float moveSpeed = 5f;
void Start()
{
initialDistance = Vector3.Distance(frontWheel1.position, frontWheel2.position);
}
void FixedUpdate()
{
float currentDistance = Vector3.Distance(frontWheel1.position, frontWheel2.position);
float scaleFactor = initialDistance / currentDistance;
Vector3 newBackWheel1Position = frontWheel1.position + (backWheel1.position - frontWheel1.position) * scaleFactor;
Vector3 newBackWheel2Position = backWheel1.position + (backWheel2.position - backWheel1.position) * scaleFactor;
backWheel1.position = Vector3.Lerp(backWheel1.position, newBackWheel1Position, moveSpeed * Time.deltaTime);
backWheel2.position = Vector3.Lerp(backWheel2.position, newBackWheel2Position, moveSpeed * Time.deltaTime);
}
}
Maybe something like this could help?