Question by
Nikhil12 · Jul 06, 2017 at 12:38 AM ·
animationanimatoranimator controllermovetowardsblender animation
animating in unity
I have cube which I move to exact location by using movetowards function(in steps of unit). Now I want that my cube should have some animation while moving to the new position. The problem I am encountering is my cube moves to the final position and then the animation starts playing not during the movement. What should I do now?(I have animated the cube in unity itself not imported the animation from other software.)
public class playermovement : MonoBehaviour {
public float playerspeed=10f;
Vector3 moven;
bool ismoving=false;
public Animator anim;
void Start () {
anim = GetComponent<Animator> ();
}
void Update () {
if (Input.GetAxis ("Horizontal") > 0) {
moven = new Vector3 (transform.position.x+1,1,transform.position.z);
ismoving = true;
anim.Play ("move"); // I have made this animation in unity itself.
}
if (Input.GetAxis ("Horizontal") < 0) {
moven = new Vector3 (transform.position.x-1,1,transform.position.z);
ismoving = true;
}
if (Input.GetAxis ("Vertical") > 0) {
moven = new Vector3 (transform.position.x,1,transform.position.z+1);
ismoving = true;
}
if (Input.GetAxis ("Vertical") <0) {
moven = new Vector3 (transform.position.x,1,transform.position.z-1);
ismoving =true;
}
if (ismoving == true) {
moveplayer ();
}
}
void moveplayer(){
transform.position = Vector3.MoveTowards (transform.position,moven,playerspeed*Time.deltaTime);
if (transform.position == moven) {
ismoving = false;
}
}
}
Comment