Search Unity

Question Trying to make realistic car axle

Discussion in 'Physics' started by Mentos_good, Oct 19, 2020.

  1. Mentos_good

    Mentos_good

    Joined:
    Nov 1, 2018
    Posts:
    1
    Hello,

    I'm trying to make a car game/project kinda from scratch, without using wheel colliders. I want to add more details and realism so I thought I would start with a simple car axle that moves forward and backward by rotating the axle (which in turn will rotate the wheels, I've used 2 cylinders for the wheels and fixed them to the axle with fixed joints)

    I already encountered some problems, when I accelerate the axis, it starts steering to the right a little, even though all component objects have the default rotation (y axis) on 0.

    Also from my understanding if I apply rigidbody torque on the X axis, the axis should always rotate forward (or backward) to it's facing direction, but when the axle rotates on the y axis the force no longer spins the axle on the x axis. (This might be my fault for not understanding the addTorque function, but I'd appreciate some help)

    TL;DR Axle with fixed joints as wheel starts steering left even though i only apply torque forward

    All help and suggestions are welcome.

    My Move script used on the axle :
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Move : MonoBehaviour
    6. {  
    7.     public Rigidbody rb;
    8.     public float speed;
    9.     private void Start()
    10.     {
    11.         rb = GetComponent<Rigidbody>();
    12.         rb.maxAngularVelocity = 100000;
    13.     }
    14.     // Update is called once per frame
    15.     void FixedUpdate()
    16.     {
    17.         Vector3 vector = new Vector3(1f, 0f, 0f);  
    18.         if (Input.GetKey(KeyCode.W))
    19.             rb.AddTorque(vector * speed * Time.deltaTime);
    20.         else if (Input.GetKey(KeyCode.S))
    21.             rb.AddTorque(vector * -speed * Time.deltaTime);
    22.            
    23.  
    24.     }
    25. }
    26.