- Home /
Idle Animation and Hold to Move
Hello, im a new unity user and trying make a simple game while learning.
I have 2 problem in my code.
1.My character uses all animations well but when it stops "run" animation, "idle" animation doesn't start. "idle" animation only starts when i replay scene (before "run" start). I think i did something wrong and character doesn't stop running animation even it looks like "run" stopped.
How can i make sure "run" animation stop?
Seems like this is wrong.
if ((transform.position - destination).magnitude < 0.7f)
{
_animator.CrossFade("idle", 0.03f);
2.Click to Move works good but i want my character follow mouse cursor if i click+hold it. Current is i have to re-click to change character's destination. I want add hold to move.
Here my code.
using UnityEngine;
using System.Collections;
using System.Security.Permissions;
public class ClickToMove : MonoBehaviour
{
public int speed = 1; // Determines how quickly object moves towards position
public float rotationSpeed = 2f;
private Vector3 targetPosition;
private Animator _animator;
private Vector3 destination;
private Quaternion targetRotation;
public float clickDelta = 0.35f; // Max between two click to be considered a double click
private bool click = false;
private float clickTime;
void Start()
{
_animator = GetComponent<Animator>();
_animator.CrossFade("idle", 0.03f);
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Mouse0))
{
if (click && Time.time <= (clickTime + clickDelta))
{
_animator.CrossFade("walk", 0.03f);
click = false;
}
else
{
click = true;
clickTime = Time.time;
}
_animator.CrossFade("run", 0.03f);
Plane playerPlane = new Plane(Vector3.up, transform.position);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float hitdist = 0.0f;
if (playerPlane.Raycast(ray, out hitdist))
{
Vector3 targetPoint = ray.GetPoint(hitdist);
targetPosition = ray.GetPoint(hitdist);
targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
destination = targetPosition;
}
}
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, rotationSpeed);
if ((transform.position - destination).magnitude < 0.7f)
{
_animator.CrossFade("idle", 0.03f);
}
}
}
Your answer
Follow this Question
Related Questions
Object colour at a location 2 Answers
Skin issues with Spine 1 Answer
How do I hide methods from the animation event inspector window? 2 Answers