Animator problem
I have this 2D topdown game where I have a click to move script attached to my player. I included animation/blend tree to my player in the click to move script. At first it didn't work, the animation didn't play when I clicked somewhere, it only worked when I move my mouse around, which would be difficult when I build my game on to a mobile phone. I then change my script to make my player follow where my mouse moved, which my animation worked, but how would I get my animator to play animation to where I clicked and not play animation to where my mouse move/hovered. Thank you. Here's my script:
using UnityEngine;
using System.Collections;
public class move : MonoBehaviour {
private Animator anim;
public float speed = 15f;
public move playerMovementRef;
private Vector3 target;
private Vector3 playerObject;
void Start () {
target = transform.position;
anim = GetComponent<Animator> ();
}
void Update () {
if (Input.GetMouseButtonDown(0)) {
target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
target.z = transform.position.z;
}
transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
float inputX = Input.GetAxis ("Mouse X");
float inputY = Input.GetAxis ("Mouse Y");
if (Input.touchCount > 0)
{
inputX = Input.touches[0].deltaPosition.x;
inputY = Input.touches[0].deltaPosition.y;
}
anim.SetFloat ("SpeedX", inputX);
anim.SetFloat ("SpeedY", inputY);
}
void FixedUpdate () {
target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
target.z = transform.position.z;
float LastInputX = Input.GetAxis ("Mouse X");
float LastInputY = Input.GetAxis ("Mouse Y");
if (Input.touchCount > 0)
{
LastInputX = Input.touches[0].deltaPosition.x;
LastInputY = Input.touches[0].deltaPosition.y;
}
if (LastInputX != 0 || LastInputY != 0) {
anim.SetBool ("walking", true);
if (LastInputX > 0) {
anim.SetFloat ("LastMoveX", 1f);
} else if (LastInputX < 0) {
anim.SetFloat ("LastMoveX", -1f);
} else {
anim.SetBool ("walking", false);
}
if (LastInputY > 0) {
anim.SetFloat ("LastMoveY", 1f);
} else if (LastInputY < 0) {
anim.SetFloat ("LastMoveY", -1f);
} else {
anim.SetFloat ("LastMoveY", 0f);
}
} else {
anim.SetBool ("walking", false);
}
}
}
Your answer
Follow this Question
Related Questions
Animation is not playing eventhough state shows it 0 Answers
How to link a follow script to the existing ThirdPersonCharacter animator 1 Answer
Animation not working, invalid layer index "-1" 1 Answer
The animations not run on the Copied/duplicated enemies (Characters) 0 Answers
Problem with game animation for gameobject movement 0 Answers