Can't seem to move my character.
I'm making a point and click game and I'm struggling to figure out how to make my character 'flip' when its moving the opposite way. I've created a walking animation but don't really know how to turn him around. Here's my player and animController script.
public class PlayerMovement : MonoBehaviour
{
public NavMeshAgent playerAgent;
void Start()
{
playerAgent = this.gameObject.GetComponent<NavMeshAgent>();
}
void Update()
{
PlayerInput();
}
private void PlayerInput()
{
if (Input.GetKeyDown(KeyCode.Mouse0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(
Input.mousePosition
);
if(Physics.Raycast(ray, out hit, 100))
{
GameObject interactedObject = hit.collider.gameObject;
if(interactedObject.tag == "Interactable Object")
{
interactedObject.GetComponent<Interactable>().MoveToInteraction(playerAgent);
}
else
{
playerAgent.stoppingDistance = 0f;
playerAgent.destination = hit.point;
}
}
}
}
}
Animation Controller
public class AnimController : MonoBehaviour
{
private NavMeshAgent agent;
private Animator anim;
// Start is called before the first frame update
void Start()
{
anim = this.gameObject.GetComponent<Animator>();
agent = this.gameObject.GetComponentInParent<NavMeshAgent>();
}
// Update is called once per frame
void Update()
{
if(anim != null && agent != null)
{
float vel = agent.velocity.magnitude;
vel = Mathf.Abs(vel);
anim.SetFloat("Velocity", vel);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Depending on the mouse movement (or position), the player sprite 0 Answers
Animator prevents OnMouseDown() 0 Answers
Help! "Assertion failed on expression: 'mem->m_ConstantClipValueCount" 0 Answers
Why default animation state rewrites scale of an object? 0 Answers
Having problems with making a Navmesh Agent stop and play an attack animation 0 Answers