Animating tank tracks

I’m trying to animate the textures on a tank’s tracks. Here’s my simple script:

var treadMat:Material;

function Update () {
	
	var vel:Vector3 = transform.root.InverseTransformDirection(transform.root.rigidbody.velocity);
	if(mat.mainTextureOffset.x - -vel.z/1000 > 1){
		mat.mainTextureOffset.x = (mat.mainTextureOffset.x - -vel.z/1000) - 1;
	}else if(mat.mainTextureOffset.x - -vel.z/1000 < -1){
		mat.mainTextureOffset.x = (mat.mainTextureOffset.x - -vel.z/1000) + 1;
	}else{
		mat.mainTextureOffset.x -= -vel.z / 1000;
	}
}

Looks pretty good, the only problem is that it doesn’t just animate the tracks on the tank that is moving, but on all tanks that use the same material.

How can I create sort of an instance of the material for each tank separately, so that only the tracks on a tank that is moving are animated?

Dont know how to make a instance of a material but you could try haveing 2 materials one animated and one stoped.

mat1
mat2

if moveing use mat1
else use mat2

I have not played around with materials yet so have very limeted experiance with them. :?

Oh yeah try messing around with

renderer.material

Since I ran into a issue with that flooding more materials into the sceen when i was makeing a visNavmesh color changer. I think that refers to the Gameobjects mat instead of the mat you have in your asset folder.

Change “mat.mainTextureOffset” to “renderer.material.mainTextureOffset”. You don’t want to animate the material from the project, you just want to animate the material on that particular object.

–Eric

That’s exactly what I was looking for, thanks.

Hello! I have created a package tank for Unity3D. It contains a 3D model of a Panzer III, the necessary scripts for motion and shot, and the wheels animation and sound. Also includes a view from inside the tank!
This package is in Buy Plugins & Code from CodeCanyon

I hope you enjoy it =)

I purchased his tank package. It’s is very basic. Here is what I’ve found.

  1. The wheels do not have colliders. The track doesn’t move.
  2. The turret is turned with the arrow keys. The tank is turned with the AWDS keys. But the arrow keys also influence the movement of the tank confusing the total movement. For example, if I want to raise the turret, the tank speeds up. If I want to lower the turret, the tank goes backwards.
  3. The projectile is a simple white sphere. It bounces along the terrain when fired. It doesn’t explode.
  4. The tank has no decent texture.
  5. The model is not in FBX format. It’s in Maya format. But if you ask the developer for an FBX file, he’ll send you one.