using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class WorldInteraction : MonoBehaviour
{
public int speed = 5;
public float rotationSpeed = 5f;
private Animator _animator;
NavMeshAgent playerAgent;
private void Start()
{
_animator = GetComponent<Animator>();
_animator.CrossFade("Idle", 0);
playerAgent = GetComponent<NavMeshAgent>();
}
private void Update()
{
if (Input.GetMouseButtonDown(0) && !UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject())
GetInteraction();
}
void GetInteraction()
{
_animator.CrossFade("Walk", 0);
Ray interactionRay = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit interactionInfo;
if (Physics.Raycast(interactionRay, out interactionInfo, Mathf.Infinity))
{
GameObject interactedObject = interactionInfo.collider.gameObject;
if (interactedObject.tag == "")
{
_animator.CrossFade("Idle", 0);
Debug.Log("Interactable interacted.");
}
else
{
playerAgent.destination = interactionInfo.point;
}
}
}
}
I have some problems:
The first problem is when i’m using the animator when running the game the player is spinning on place nonstop.
Without the animator part it will work without spinning. But i want it to work with the animator part.
The second problem is when i’m clicking the mouse it will go to the place but a bit front of it ahead. I want it to get to the exact mouse cursor position point i clicked.
Another thing i noticed sometimes with or without using this script when i use the keys to move the character or by mouse with the script in some places the player is like crouch very fast like something hit him or he hit something. crouch or duck not sure how to describe it but in some places when he walk/move he is like ducking/crouching or sitting for millisecond or so. Strange.