Hello, help me pls. Idk why but my character dont want to move if I click lmb or rmb it just move for 1 millimetr and then stop.
But when i press and hold lmb it works fine.
Problem video:
Mover.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
namespace RPG.Movement
{
public class Mover : MonoBehaviour
{
NavMeshAgent navMeshAgent;
private void Start(){
navMeshAgent = GetComponent<NavMeshAgent>();
}
void Update()
{
UpdateAnimator();
}
public void MoveTo(Vector3 distination)
{
navMeshAgent.destination = distination;
navMeshAgent.isStopped = false;
}
public void Stop(){
navMeshAgent.isStopped = true;
}
private void UpdateAnimator(){
Vector3 velocity = navMeshAgent.velocity;
Vector3 localVelocity = transform.InverseTransformDirection(velocity);
float speed = localVelocity.z;
GetComponent<Animator>().SetFloat("ForwardSpeed", speed);
}
}
}
Fighter.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using RPG.Movement;
namespace RPG.Combat
{
public class Fighter : MonoBehaviour
{
[SerializeField] float weaponRange = 2f; //2 meters
Transform target;
private void Update() {
if (target != null && !(Vector3.Distance(transform.position, target.position) < weaponRange))
{
GetComponent<Mover>().MoveTo(target.position);
Debug.Log("Moved to target");
}
else
{
GetComponent<Mover>().Stop();
target = null;
}
}
public void Attack(CombatTarget combatTarget){
target = combatTarget.transform;
}
}
}
PlayerController.cs
using System;
using UnityEngine;
using RPG.Movement;
using RPG.Combat;
namespace RPG.Control
{
public class PlayerController : MonoBehaviour
{
private void Update()
{
if(InteractWithCombat()) return;
if(InteractWithMovement()) return;
}
private bool InteractWithCombat()
{
RaycastHit[] hits = Physics.RaycastAll(GetMouseRay());
foreach (RaycastHit hit in hits)
{
CombatTarget target = hit.transform.GetComponent<CombatTarget>();
if(target == null) continue;
if(Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1) || Input.GetMouseButton(0)){
GetComponent<Fighter>().Attack(target);
}
return true;
}
return false;
}
private bool InteractWithMovement()
{
RaycastHit hit;
bool hasHit = Physics.Raycast(GetMouseRay(), out hit);
if (hasHit)
{
if(Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1) || Input.GetMouseButton(0)){
GetComponent<RPG.Movement.Mover>().MoveTo(hit.point);
}
return true;
}
return false;
}
private static Ray GetMouseRay()
{
return Camera.main.ScreenPointToRay(Input.mousePosition);
}
}
}
I have no idea how to fix it…