- Home /
Problem with Idle animation for player
I'm following Sykoo's Let's make an RPG series, our code is basically the same but I have a problem when executing. the game works fine but when I first drop I'm idle then I point and click to move and moving animation works fine but after I reach my destination the player keeps on doing the walking animation and doesn't go back to idle. This is the code. using System.Collections; using System.Collections.Generic; using UnityEngine;
public class playerMovement : MonoBehaviour
{
//variables
public float movementSpeed;
public GameObject playerMovePoint;
private GameObject pmr;
Animation anim;
private bool triggeringPMR;
bool moving;
public void Start()
{
pmr = Instantiate(playerMovePoint, this.transform.position, Quaternion.identity);
anim = GetComponent<Animation>();
}
//functions
public void FixedUpdate()
{
//Player Controls
Plane playerPlane = new Plane(Vector3.up, transform.position);//Creating a plane and detecting mouse click on it with ray
Ray ray = UnityEngine.Camera.main.ScreenPointToRay(Input.mousePosition);// stores mouse position into ray
float hitDistance = 0.0f;
if (playerPlane.Raycast(ray, out hitDistance))
{
Vector3 mousePosition = ray.GetPoint(hitDistance);
if (Input.GetMouseButtonDown(1))
{
moving = true;
triggeringPMR = false;
pmr.transform.position = mousePosition;
pmr.GetComponent<BoxCollider>().enabled = true;
}
}
if (moving)
Move();
else
Idle();
if (triggeringPMR)
{
moving = false;
}
}
public void Move()
{
transform.position = Vector3.MoveTowards(transform.position, pmr.transform.position, movementSpeed);
this.transform.LookAt(pmr.transform);
anim.CrossFade("walk");
}
public void Idle()
{
anim.CrossFade("idle");
}
public void OnTriggerEnter(Collider other)
{
if (other.tag == "PMR")
{
triggeringPMR = true;
}
}
public void OnTriggerExit(Collider other)
{
if (other.tag == "PMR")
{
triggeringPMR = false;
}
}
}
Can you post a screenshot of your animator component? The issue is likely with your transitions in that.
Answer by jonorgan26 · Dec 30, 2019 at 10:45 PM
Unless I'm missing something is there a reason you don't have a { on line 44 and after the "else" on line 46, and a } before the "else" on line 46 and on line 49 as well?
Your answer
Follow this Question
Related Questions
Animation doesn't work 1 Answer
How do i orginize 8+ movement animations? 1 Answer
How to loop unity?JS 3 Answers
Can the animation editor create local rotational data? 3 Answers
Adding animation clips via script 2 Answers