Search Unity

Resolved transform.localRotation on wheels for a tank

Discussion in 'Animation' started by GreatWhiteBuffalo, Apr 15, 2022.

  1. GreatWhiteBuffalo

    GreatWhiteBuffalo

    Joined:
    Apr 14, 2022
    Posts:
    4
    Hello! I downloaded unity yesterday for a school project and I am completly new to this type of development.
    I have a tank model with 8 wheels on each side. These are children to the tank body that is a rigidbody.
    The labhint says that when the tank moves, so should the wheels and we are given a hint to use transform.localRotation to make this happen. This is how I went about it.

    // Rotation in the x axis.
    Quaternion wheelRotation = Quaternion.Euler(turnRate, 0f, 0f);

    for(var i = 0;i<m_wheels.Count;i++){
    m_wheels.transform.localRotation= wheelRotation;
    }

    the turnRate is just a float value that depend on if the tank is moving or not.

    I'm obviously not going about this the right way, any tips on how to proceed?


    I apologize in advance for any obvious mistakes that I have made!(let me know if this should be posted in another forum instead of animation)
     
  2. Flylake

    Flylake

    Joined:
    Feb 26, 2014
    Posts:
    12
    Probably not posted in the right forum, but I'll chime in either way

    Unless you have an accumelator, and turnRate doesn´t grow each iteration, you´ll be creating identical quaternions every iteration

    Add a new variable, and add turnRate to it every update, and use that new variable instead of turnRate when you create your quaternion.

    Alternatively you can look into the Transform.Rotate method on your wheel.
    ie.

    m_wheels.transform.Rotate(new Vector3(turnRate,0f,0f), Space.Self)
     
  3. GreatWhiteBuffalo

    GreatWhiteBuffalo

    Joined:
    Apr 14, 2022
    Posts:
    4
    Thanks for the fast reply, greatly appreciated! I will try both solutions
     
  4. GreatWhiteBuffalo

    GreatWhiteBuffalo

    Joined:
    Apr 14, 2022
    Posts:
    4
    After trying to implement both solutions I'm still not getting any result, I created a new variable and added turnrate to it every update but my wheels remain static, same when using transform.Rotate, I feel like your solution with adding a new variable should work and I am confused as to why it doesn't.


    This is what it currently looks like:
    public float turnRate2; (global variable)
    turnRate = m_WheelRotateSpeed * m_MovementInputValue * Time.deltaTime;(gets called in the update function)

    private void RotateWheels()
    {


    turnRate2 = turnRate2 + turnRate;

    var wheelRotation = Quaternion.Euler(turnRate2, 0f, 0f);

    for (var i = 0; i < m_wheels.Count; i++)
    {
    m_wheels.transform.localRotation = wheelRotation;
    }

    This is all part of a script for controlling the tank
     
    Last edited: Apr 16, 2022
  5. Flylake

    Flylake

    Joined:
    Feb 26, 2014
    Posts:
    12
    Strange - I´d start checking my variables to see if anything is out of place

    make sure that your turnRate variable actually gets a set value each update

    make sure that your m_wheels array is actually populated with references to the actual wheels and not just prefabs
     
  6. GreatWhiteBuffalo

    GreatWhiteBuffalo

    Joined:
    Apr 14, 2022
    Posts:
    4
    There was something wrong with turnRate, my guess is that the value I assigned it was too small, when assigning turnrate with a static value the wheels spin as intended! Much thanks for the help, hopefully I can do the same for someone on here someday. Happy easter!