Animation wont play (click to move)
using UnityEngine;
using System.Collections;
public class ClickToMove : MonoBehaviour {
private Transform myTransform; // this transform
private Vector3 destinationPosition; // The destination Point
private float destinationDistance; // The distance between myTransform and destinationPosition
public float moveSpeed; // The Speed the character will move
public AnimationClip idle;
public AnimationClip run;
public AnimationClip punch;
void Start () {
myTransform = transform; // sets myTransform to this GameObject.transform
destinationPosition = myTransform.position; // prevents myTransform reset
}
void Update () {
if (Input.GetKeyDown (KeyCode.X)) {
GetComponent<Animation> ().Play (punch.name);
}
// keep track of the distance between this gameObject and destinationPosition
destinationDistance = Vector3.Distance(destinationPosition, myTransform.position);
if (destinationDistance < .5f) { // To prevent shakin behavior when near destination
moveSpeed = 0;
GetComponent<Animation> ().Play (idle.name);
} else if (destinationDistance > .5f) { // To Reset Speed to default
moveSpeed = 30;
GetComponent<Animation> ().Play (run.name);
}
// Moves the Player if the Left Mouse Button was clicked
if (Input.GetMouseButtonDown(0)&& GUIUtility.hotControl ==0) {
Plane playerPlane = new Plane(Vector3.up, myTransform.position);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float hitdist = 0.0f;
if (playerPlane.Raycast(ray, out hitdist)) {
Vector3 targetPoint = ray.GetPoint(hitdist);
destinationPosition = ray.GetPoint(hitdist);
Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
myTransform.rotation = targetRotation;
}
}
// Moves the player if the mouse button is hold down
else if (Input.GetMouseButton(0)&& GUIUtility.hotControl ==0) {
Plane playerPlane = new Plane(Vector3.up, myTransform.position);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float hitdist = 0.0f;
if (playerPlane.Raycast(ray, out hitdist)) {
Vector3 targetPoint = ray.GetPoint(hitdist);
destinationPosition = ray.GetPoint(hitdist);
Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
myTransform.rotation = targetRotation;
}
// myTransform.position = Vector3.MoveTowards(myTransform.position, destinationPosition, moveSpeed * Time.deltaTime);
}
// To prevent code from running if not needed
if (destinationDistance > .5f) {
myTransform.position = Vector3.MoveTowards (myTransform.position, destinationPosition, moveSpeed * Time.deltaTime);
}
}
}
hi, idle and run animations Works fine.. but i want play punch animation when i press X... and not wokring.. i dont understans...
sory for my engrish.. help me pls
Answer by robicro · Jun 04, 2017 at 03:30 PM
Help me plsss
i dont know if this is the issue, but ins$$anonymous$$d of doing GetComponent () .Play i would make declare a variable anim and in the Start function put anim = Getcomponent() then just do anim.Play() whenever you want to play it. This is just syntax, not actually your error (probably)
Your answer
Follow this Question
Related Questions
Onde encontro a pasta "projet's assets"? 0 Answers
Creating AnimationClip Using Custom Values 1 Answer
How can I create animation keyframes using C# scripting? 0 Answers
Animation clips using different start value for Y 0 Answers
Animation Switch... 2 Answers