Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Car Steer Angle

Discussion in 'Scripting' started by MattBee, Mar 4, 2014.

  1. MattBee

    MattBee

    Joined:
    Aug 23, 2013
    Posts:
    27
    Hi, I'm wanting to know the correct way to convert this piece of javascript code to C#:

    Code (csharp):
    1. var steerVector : Vector3 = transform.InverseTransformPoint(Vector3(path[currentPathObj].position.x,transform.position.y,path[currentPathObj].position.z));
    2.  
    3. //Rest of code for steer angle
    4. var newSteer : float = maxSteer * (steerVector.x / steerVector.magnitude);  
    5. wheelFL.steerAngle = newSteer;  
    6. wheelFR.steerAngle = newSteer;  
    7.  
    I've did the following:

    Code (csharp):
    1.     Vector3 steerVector = transform.InverseTransformPoint(path[currentPathObj].position.x, transform.position.y, path[currentPathObj].position.z);
    2.  
    3. //Rest of code for steer angle
    4. float newSteer = maxSteer * (steerVector.x / steerVector.magnitude);
    5.         WheelFL.steerAngle = newSteer;
    6.         WheelFR.steerAngle = newSteer;
    7.  
    And it seems to perform as it should but the problem is the Wheel Colliders, when selected, don't visually turn so when it comes to doing the visual steering of the wheel meshes, it won't work because it's using the colliders steer angle which seems to be stationary.

    The car will still drive around and turn as it should.

    Note: This is for an AI car following a path, just in case it's useful information.

    All answers are appreciated, thanks ;)
     
    Last edited: Mar 4, 2014
  2. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    What youre saying doesnt make a lot of sense.

    "And it seems to perform as it should but the problem is the Wheel Colliders, when selected, don't visually turn "

    is a contradiction to

    "The car will still drive around and turn as it should."

    simply because the car shouldnt turn if the wheel colliders dont (they do visually turn if you set a steer angle)
     
  3. MattBee

    MattBee

    Joined:
    Aug 23, 2013
    Posts:
    27
    I'll try and explain what I mean.

    The steer angles work as they should, the car will turn X amount to the right or left when needed but if the wheel colliders are selected from within the hierarchy while the game is running, the wheel colliders themselves don't visually turn i.e. the WheelCollider Game Object just stays stationary.

    It is an awkward thing to try and explain.

    Is my code from the first line of the second code block correctly converted from the first line of the first code block? If so then the problem must lie elsewhere.

    Maybe it's a problem with taking this:

    Code (csharp):
    1. transform.localEulerAngles.y = myWheelCollider.steerAngle - transform.localEulerAngles.z;  
    2.  
    and making it this?

    Code (csharp):
    1. transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, myWheelCollider.steerAngle - transform.localEulerAngles.z, transform.localEulerAngles.z);
    2.  
    I don't believe that is the problem though. This is all the code related to the steer angle of the wheel colliders and the wheel meshes.
     
    Last edited: Mar 4, 2014
  4. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    How is the car turning/moving?

    Are you using the motorTorque to drive the car?
     
  5. MattBee

    MattBee

    Joined:
    Aug 23, 2013
    Posts:
    27
    Yeah. It's using similar mechanics to the player car with some differences since it's not player controlled. I can't figure out why it won't turn, everything seems to be set the same way the player car handles steering and it works in the tutorial I'm following.

    If all the code I've posted is correctly converted then the problem must lie elsewhere.
     
  6. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    here are some snippets from my turning code...

    this is from the AiDriver... (note: agent is a NavMeshAgent, and steering target is just a Vector3 location)
    Code (csharp):
    1.  
    2.   var relativePos = agent.steeringTarget - transform.position;
    3.   targetRotation = Quaternion.LookRotation(relativePos);
    4.  
    5.    float y = transform.eulerAngles.y;
    6.    DeltaAngle = Mathf.DeltaAngle(y, targetRotation.eulerAngles.y)
    7.  
    8.    float delta = Mathf.Clamp(DeltaAngle, -1, 1);
    9.    engine.Turn(delta);
    10.  
    11.  
    Calls into the EngineController code
    Code (csharp):
    1.  
    2. public void Turn(float turn)
    3. {
    4.     CurrentTurn = turn * MaxTurnAngle;
    5.      if(axleController)
    6.          axleController.SetSteering(CurrentTurn);
    7. }
    8.  
    9.  
    Calls into the AxleController code
    Code (csharp):
    1.  
    2. public void SetSteering(float amount)
    3. {
    4.            for (int x = 0; x < WheelControllers.Count; x++)
    5.               WheelControllers[x].SetSteering(amount);
    6. }
    7.  
    Which finally calls into a WheelController..
    Code (csharp):
    1.  
    2. public void SetSteering(float amount)
    3. {
    4.   if(IsTurningWheel)
    5.    {
    6.      if(wheelCollider.steerAngle < amount)
    7.         wheelCollider.steerAngle++;
    8.      else if(wheelCollider.steerAngle > amount)
    9.       wheelCollider.steerAngle--;
    10.     }
    11. }
    12.  

    Hopefully that helps.
     
  7. MattBee

    MattBee

    Joined:
    Aug 23, 2013
    Posts:
    27
    Thanks for the code, much neater and better done than what I have. I'll try to use it to fix my AI mesh steering tomorrow and let you know if it worked.

    It's really weird because the player car will do the mesh turning just fine.

    Were my code snippets in the first post converted correctly?
     
  8. Wowmaniac

    Wowmaniac

    Joined:
    Jun 27, 2014
    Posts:
    4
    Objects the wheel Colliders are attached to don't turn when changing the steerAngle, you MUST make separate objects to act as the graphical wheels and apply the same rotation as the Colliders. The same must be done with the suspension using collider.wheelHit to simulate the wheel movement when going over bumps.