Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

4 Wheeled Farm Wagon Steering?

Discussion in 'General Discussion' started by markasuter, Mar 15, 2021.

  1. markasuter

    markasuter

    Joined:
    May 20, 2019
    Posts:
    22
    wagon_turn.PNG
    I want a wagon with 4 wheels where the hitch will follow the truck and cause the wagon to turn it's front wheels pivot point as shown in the picture.

    I couldn't find such a wagon so I'm trying to build one myself, but I don't know how to accomplish this. Maybe some hinges? The trailer should follow the truck realistically as it would in the real world, whether the truck is moving backward or forward. Any ideas? Thanks!
     
  2. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,793
    Try finding the vector between the hitch bulb and the wagon hitch pivot. As well find the rest distance length of that distance between ball and hitch pivot and in your update keep that constant. The derived vector would be used to set the steering rotation of the front wagon wheels. It seems to me that if you updated these two values ..keep the distance constant and change the vector of the steering algo then it should reasonably simulate how it works without messing with joints etc.
     
  3. markasuter

    markasuter

    Joined:
    May 20, 2019
    Posts:
    22
    I understand about 1/3 of what you said, sorry. So through googling, here's my first attempt at the script:
    Code (CSharp):
    1.  
    2.     public Transform wagonJoint;
    3.     public Transform ballHitch;
    4.     public GameObject frontWheelLeft;
    5.     public GameObject frontWheelRight;
    6.  
    7.     void Update()
    8.     {
    9.         //find the angle between the ball hitch and wagon pivot joint
    10.         Vector3 wagonDir = wagonJoint.position - ballHitch.position;
    11.         float angle = Vector3.Angle(wagonDir, transform.forward);
    12.         frontWheelLeft.transform.eulerAngles = new Vector3(0,angle+90,0);
    13.         frontWheelRight.transform.eulerAngles = new Vector3(0, angle+90, 0);
    14.  
    15.         //Rest Distance Length from ball hitch to wagon pivot joint
    16.         float restDist = Vector3.Distance(wagonJoint.position, ballHitch.position);
    17.  
    18.         Debug.Log("wagonDir: " + wagonDir + "& angle = " + angle);
    19.     }
    and in play mode, the tires go offset in position as shown below, and when I move the tongue of the wagon, the tires rotate in the opposite direction they should be rotating, also shown below.
    inverse.PNG
    Inverse steering:
    inverse2.PNG
    Also, I'm not sure once I get this steering figured out, how the wagon will actually respond to the direction of the front tires, or will just having rigidbody's on them achieve that?
     
  4. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,793
    You will probably have to multiply one of your values for angles by -1. The rigidbodies should help with it as you guessed. This was not a solution I proposed .it was a method to find a solution by using a main mechanic I can shuffle numbers on to get a reasonable sim of the behavior I am attempting to fake. The wagon wheels should be pointing at the hitch. The hitch bar should always be attached to the tow bar ball and the wagon hitch pivot. Get that down first..then the wheels should easily follow.