Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Gooren · Jan 02, 2016 at 08:20 AM · animationrotationmovementtankwheels

Tank Wheels and Treads

Hey guys, I am working on a mobile game where tanks play an important role. Each tank has it's own NavMeshAgent component that is being used to move it from A to B. And that works pretty well for me. However in order to add more realism i would also like to rotate wheels and spin the Treads according to NavMeshAgent's current velocity and angularVelocity. There is a problem though since i don't know it's current angularVelocity. There is only max/desired value available. Final formula I am looking for should contain NavMeshAgent's velocity, angularVelocity and wheel center distance from the center of the NavMeshAgent (the tank in our case), is that right? I am really quite clueless here :(

Currently i have this script for wheels:

 public class TankWheel : MonoBehaviour
 {
     private NavMeshAgent _navMeshAgent;
     private float _radius;

     void Awake()
     {
         _navMeshAgent = GetComponentInParent<NavMeshAgent>();
         SphereCollider sphereCollider = gameObject.AddComponent<SphereCollider>();
         _radius = sphereCollider.radius; // TODO: find a better way of wheel radius estimation
         Destroy(sphereCollider);
     }
 
     void FixedUpdate()
     {
         float distanceTraveled = _navMeshAgent.velocity.magnitude * Time.deltaTime;
         float rotationInRadians = distanceTraveled / _radius;
         float rotationInDegrees = rotationInRadians * Mathf.Rad2Deg;
 
         transform.Rotate(rotationInDegrees, 0, 0);
     }
 }

which seems to work "perfectly" except it does not add angularSpeed and the (wheel center - NavMeshAgent center) distance into consideration, and I also have this script for treads:

 public class TankBelt : MonoBehaviour
 {
     private const string textureName = "_MainTex";
 
     private Renderer _renderer;
     private NavMeshAgent _navMeshAgent;
     private float _textureOffsetX;
     
     void Awake()
     {
         _renderer = GetComponent<Renderer>();
         _navMeshAgent = GetComponentInParent<NavMeshAgent>();
     }
     
     void FixedUpdate()
     {
         float distanceTraveled = _navMeshAgent.velocity.magnitude * Time.fixedDeltaTime;

         _textureOffsetX += distanceTraveled * 0.05f; // 5% - magic, seems to works in our specific scenario

         _renderer.material.SetTextureOffset(textureName, new Vector2(_textureOffsetX, 0f));
     }
 }

which is a bit hacky but again - at least seems to work... yet still without angularVelocity being taken into consideration. All I am trying to achieve here is just making the impression of movement being performed by wheels and treads when it's not really the case since all the movement/rotation of the tank is being performed by the NavMeshAgent component only.

It is also performance friendly for us. I will be extremely glad for any help. Thank You, Steven

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by joebain · Jun 06, 2016 at 12:19 PM

You can achieve the desired effect by storing the previous location of each wheel and then calculating the amount it should spin based on the distance it has moved along it's forward vector, rather than using the body's velocity. If you have a script for each wheel, for example:

 private Vector3 lastPosition;
 private Vector3 lastPosition;
 public float wheelRadius = 1; // you need to set this based on your model

 private Transform child; // your visual mesh should be the first child of this object

 void Start()
 {
     lastPosition = transform.position;

     child = transform.GetChild(0);
 }

 void Update()
 {
     Vector3 movement = transform.position - lastPosition;
     float distance = Vector3.Dot(movement, transform.forward);
     float spin = (distance / (2f*Mathf.PI *wheelRadius)) * 360f;
     child.Rotate(new Vector3(-spin, 0, 0)); // assuming your wheel models are facing z-forward, y-up

     lastPosition = transform.position;
 }

You can also use this distance to animate your tank tracks, by having this object at the centre position of each track and converting the distance to a texture offset as you were before. If you are moving the tracks and wheels at the same time you may want to move all the wheels on each side based on a single controller which also moves the tracks - that way the distance will always be the same, and the tracks and wheels should move the same amount.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Gooren · Oct 13, 2016 at 08:32 AM 0
Share

Thank You so much! And sorry for my late reply.

avatar image Acreates · Oct 25, 2018 at 06:30 AM 0
Share

Question, is there a reason why this is defined twice? private Vector3 lastPosition; private Vector3 lastPosition;

avatar image Gooren Acreates · Oct 25, 2018 at 08:01 AM 0
Share

It definitely had to be just a copy/paste mistake.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How could I play the correct animations for a character who always rotates to look at the mouse, but takes World relative input? 0 Answers

Character still walking when no key is already pressed 1 Answer

How to make animations match movement direction 0 Answers

How can I straighten my character's movement and actions? 0 Answers

made a ring collider from sphere colliders and a bar collider, but when animated the ring doesn't stay on bar 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges