- Home /
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
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.
Question, is there a reason why this is defined twice? private Vector3 lastPosition; private Vector3 lastPosition;
It definitely had to be just a copy/paste mistake.