Player and Enemy not moving

My Player and Enemy aren’t moving, since I have replaced the objects with the Prefab Variants

1 Like

Make sure your Player variant has a PlayerController, and your Enemy variant has an AI Controller. The parent prefab should have neither of these things.

The Parent should have the Animator, Mover, Fighter and NavmeshAgent

I’m not yet at the AI Controller part of the lesson, but at the moment, both the player and enemy should should move the same way, because they both have the PlayerController.
My Character prefab (parent) has those as you said. My Player has the PlayerController.

Screenshot 2021-11-23 at 15.59.43
Screenshot 2021-11-23 at 15.59.47
Screenshot 2021-11-23 at 15.59.56



I did remove the Fighter in Player and Enemy, but when they do have Fighter, they still don’t move.

I’m assuming it was working before the change to prefab variants, and that your terrain is baked.
Are there any error messages in the console?
Paste in your Mover and Player Controller script (don’t forget to format them using the ``` backwards apostrophe) and we’ll take a look.

Alright.
I have no error messages in the console.
Mover Script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using RPG.Core;

namespace RPG.Movement
{
    public class Mover : MonoBehaviour, IAction 
    {
        [SerializeField] Transform target;

        NavMeshAgent navMeshAgent;

        private void Start() {
            navMeshAgent = GetComponent<NavMeshAgent>();
        }

        void Update()
        {
            UpdateAnimator();
        }

        public void StartMoveAction(Vector3 destination)
        {
            GetComponent<ActionScheduler>().StartAction(this);
            MoveTo(destination);
        }

        public void MoveTo(Vector3 destination)
        {
            navMeshAgent.destination = destination;
            navMeshAgent.isStopped = false;
        }

        public void Cancel() 
        {
            navMeshAgent.isStopped = true;
        }

        private void UpdateAnimator()
        {
            Vector3 velocity = navMeshAgent.velocity;
            Vector3 localVelocity = transform.InverseTransformDirection(velocity);
            float speed = localVelocity.z;
            GetComponent<Animator>().SetFloat("forwardSpeed", speed);
        }
    }
}

Player Controller Script:

using System;
using RPG.Movement;
using RPG.Combat;
using UnityEngine;


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))
                {
                    GetComponent<Fighter>().Attack(target);
                }
                return true;
            }
            return false;
            
        }

        private bool InteractWithMovement()
        {
            RaycastHit hit;
            bool hasHit = Physics.Raycast(GetMouseRay(), out hit);
            if (hasHit)
            {
                if (Input.GetMouseButton(0))
                {
                    GetComponent<Mover>().StartMoveAction(hit.point);
                }
                return true;
            }
            return false;
        }

        private static Ray GetMouseRay()
        {
            return Camera.main.ScreenPointToRay(Input.mousePosition);
        }
    }
}

Indeed, The Player moved and fought before I replaced him with the Prefabs.

As far as I can tell, the scripts are correct, so the issue is somehow in the creation of the Prefabs, or the characters in the scene aren’t properly updating with the prefabs…
Go to the player in the scene and make sure that the components match the ones in the Prefab. If they don’t, you should see that they have bold titles. You can right click at the top of the overrides button and click “Revert” to make them match the prefabs.

Hmmmm…
The components do match.
I have no idea how to solve this. This time, restarting the software didn’t help.

Zip u your project and upload it to https://gdev.tv/projectupload and I’ll take a look. Don’t forget to remove the Library folder to Ave space.

Ok, I’ve sent it, If it’s working for you, then perhaps, there’s an isuue caused by this project being on an external SSD?

Actually, there are a few…


The first one can be fixed by dragging the player into the Follow Camera script

The second two can be fixed by adding an ActionScheduler to the Character prefab. (I forgot that in my catalogue of scripts needed, it can be hard to remember every state of the game through the course, sorry about that)

The third one was a bit trickier, it turns out there was a Player Controller script on your terrain.

2 Likes

Checking now… strange, I don’t see any errors in my Console…
That solved it…

Ahh… I must have clicked on the Red Error button (stop sign with “!”) before and switched it off, so I didn’t see any errors!

Thanks, It’s now working how it is supposed to.

1 Like

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

Privacy & Terms