- Home /
RTS movement with walking animation.
Hi there!
The only animated 3D character I could get to try this engine out is the Hero from Locomotion System,... so I created a terrain, and I wrote the following script to move it around the map with the "click to move", rts-like movement.
var smooth:int; // Determines how quickly object moves towards position
private var targetPosition:Vector3; function Start () { targetPosition = transform.position; }
function Update () { var playerPlane = new Plane(Vector3.up, transform.position); var rayo = Camera.main.ScreenPointToRay (Input.mousePosition); var puntochoque = 0.0;
if(Input.GetKeyDown(KeyCode.Mouse0))
{
if (playerPlane.Raycast (rayo, puntochoque)) {
var targetPoint = rayo.GetPoint(puntochoque);
targetPosition = rayo.GetPoint(puntochoque);
var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
transform.rotation = targetRotation;
}
}
smooth = 5.0;
transform.position = Vector3.Lerp (transform.position, targetPosition, Time.deltaTime * smooth);
}
But now I'd like to add the animation of "RunForward" (incorporated in the model) in this code in order to get the walking animation while the character is moving. Maybe the way I use to move the character isn't the proper one...
So, how could I add the animation?
Thanks in advance!
Answer by IronFurball · Apr 25, 2012 at 05:16 AM
Do you still need help with this? in case you do:
if you want to play an animation, all you have to do is
Animation theWalkAnimation; //This is the name of the animation, it doesnt have to // be the same as the actual name of the animation that // is on the character.
function Start()
{
theWalkAnimation = GetComponent<Animation>(); //This gets the animation component of the gameobject the script is attached to.
}
function Update()
{
theWalkAnimation.Play();
}
by the looks of it you're using javascript, but this should work in both java and C#.