using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WorldInteraction : MonoBehaviour
{
public int speed = 5; // Determines how quickly object moves towards position
public float rotationSpeed = 5f;
public bool autoMove = true;
private Animator _animator;
private bool toMove = true;
UnityEngine.AI.NavMeshAgent playerAgent;
private void Start()
{
_animator = GetComponent<Animator>();
_animator.CrossFade("Idle", 0);
playerAgent = GetComponent<UnityEngine.AI.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 == "Interactable Object")
{
interactedObject.GetComponent<Interactable>().MoveToInteraction(playerAgent);
}
else
{
playerAgent.destination = interactionInfo.point;
}
}
}
}
The first problem is with the “Walk” mode.
If i’m not using the “Walk” mode then each time i’m clicking the mouse at some point the player will get there and stop there. But if i’m using the “Walk” mode the player will keep walking and will not stop at the mouse clicked position.
I can’t find where and how to make that if the player got to the mouse clicked position then “Idle”.
_animator.CrossFade("Idle", 0);
The second problem is i don’t want it to jump between the two modes. Walk and Idle. I want it to slowly Idle to Walk and also from Walk to Idle. So for the Idle mode i did before:
_animator.CrossFade("Idle", 2);
And it’s working fine.
But when i’m doing for the walking:
_animator.CrossFade("Walk", 2);
Then the player is not walking at all he is just moving no walking animation. Only when i set 2 for the Walk.
Why is that ? I want it change slowly from Idle to Walk.
The last problem is with the other classes i have i want that when i click on an item to interact with if the player is close enough to the item let’s say < 3 then Idle.
So i have this class:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Interactable : MonoBehaviour
{
public UnityEngine.AI.NavMeshAgent playerAgent;
public virtual void MoveToInteraction(UnityEngine.AI.NavMeshAgent playerAgent)
{
this.playerAgent = playerAgent;
playerAgent.destination = this.transform.position;
Interact();
}
public virtual void Interact()
{
Debug.Log("Interacting with base class.");
}
}
And this one an item to interact with
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PickupItem : Interactable
{
public override void Interact()
{
Debug.Log("Interacting with Item.");
}
}
I don’t want the player to collide with the item but to idle just a bit before reaching it. But close enough to interact with it.