RPG course lecture 37 - Player Shaking when approaching the Enemy

Hi, after copy paste the Player controller component in the player prefab. My player can move, but when it stops walking, it starts shaking and rotating a bit as I’m near the enemy. If I move the player far away, it stops shaking.

It doesn’t look like the problem comes from the animation, because if I untick it, my player continue to shake in T pose.

Thanks in advance for your help and sorry for my poor english.

I’m not sure what could be in the PlayerController that could be causing this behaviour…
Can you post a video of what’s going on?
Paste in your PlayerController.cs script as well and we’ll take a look.

Yes, I tried to untick the nav Mesh Agent of the enemy and the shakings are no more. But I don’t think it should remain untick for the next.

here is a video of my problem, I hope it is visible enough. https://youtu.be/7yPhf4gHNZM

and my Player Controller :

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

namespace RPG.Control
{
    public class PlayerController : MonoBehaviour
    {
        private void Update()
        {
            if (InteractWithCombat()) return;
            if (InteractWithMovement()) return;
            print("Nothing to do!");
        }

        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);
        }
    }
}

Thanks for your quick reply.

If the issue is solved by turning off the enemy’s NavMeshAgent (not really a solution, per se, as the enemy WILL need an NavMeshAgent), I think the issue that when the two NavMeshAgents meet, they are competing for locations that are two close together.

You might check the distance to the target (GetComponent<NavMeshAgent>().remainingDistance) and compare it to an acceptance radius. Something smaller than the attack range, but larger than the NavMeshAgent’s radius.

Privacy & Terms