- Home /
Animating Tank Treads
I'm currently working on a project that also involves rendering a tank model and I have a question regarding the tank treads. I've got it working by simply animating the offset of the texture applied to the tank treads and everything looks fine but I'm not sure if my approach could fail under certain conditions. Could anyone take a look and tell me if it makes sense what I am doing here:
void FixedUpdate () { AnimateTreads(); }
void AnimateTreads() { var velocity = rigidbody.velocity.magnitude * 0.01f;
if(velocity > 0)
{
// transform velocity into local space to figure out if we are moving backward or forward
var localVelocity = transform.InverseTransformDirection(rigidbody.velocity);
// tank is moving forward apply negative tread texture offset
if(localVelocity.z < 0)
{
treadLeft.material.mainTextureOffset -= new Vector2(0.0f, velocity);
treadRight.material.mainTextureOffset -= new Vector2(0.0f, velocity);
}
// tank is moving backward apply positive tread texture offset
else
{
treadLeft.material.mainTextureOffset += new Vector2(0.0f, velocity);
treadRight.material.mainTextureOffset += new Vector2(0.0f, velocity);
}
}
} }
Your design looks fine. However, I suggest testing. There's no way to know if your logic is going to fail without spending the time testing it over time.
Also, remember to keep track of your variables. As long as you don't set those variables anywhere you don't mean to, you should be good to go.
Your answer
Follow this Question
Related Questions
Look at player during animation 0 Answers
Can the animation editor create local rotational data? 3 Answers
Adding animation clips via script 2 Answers
[2D]Tank track animation 0 Answers