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 hereCurrently 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