The character does not move normally after the 28th lesson

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…

I don’t know where you are in the course because I don’t know which lesson is the 28th lesson but your Fighter script stops the movement if it has no target so, pressing the mouse button will move it but then the Fighter script will immediately stop it from moving. I went through a few lectures and I don’t see Sam having the same code in the lectures. Perhaps go through the lectures again and make sure you follow it correctly.

Imma sure that everythin is same like in lecture 'cause I tried everything, at least i just copied code from git and got same issue

Like I said, I don’t know which is the 28th lesson but I found one that is somewhat similar to yours (Cancelling Combat With Movement). However, Sam has a Cancel method in Fighter which you don’t have. He also has a StartMoveAction in Mover that you don’t have. Sam also only uses the ‘hold to move’ so you won’t see the same issue in the videos because he doesn’t implement a ‘click to move’, but he has them.

OHHHH MAN RLY???
Omg thank you so much, i didnt saw that he used only hold to move…
man rly thank you, u just saved all my time

Actually, it’s click to move… as long as you keep holding the mouse down, though, it will register that and continue moving you to the new cursor loctation.

Your issue is in the Fighter component.
Start the Update() method with

if(target==null) return;

and this will prevent Fighter from cancelling the movement when there’s no target.

1 Like

Thank you so much, it helped! <3

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms