AI strafe while aiming, code change recommendation

Leorid

New member
Maybe you want to change those lines in your next update, so it is easy to get the enemy AI-character moving in all directions while aiming:

at the script "NavMeshAgentMovement" at Line Nr 70, inside the IF with the comment "// Only move if a path exists."

From:
C#:
                        lookRotation = Quaternion.LookRotation(m_NavMeshAgent.desiredVelocity, m_CharacterLocomotion.Up);
                        // The normalized velocity should be relative to the target rotation.
                        var velocity = Quaternion.Inverse(lookRotation) * m_NavMeshAgent.desiredVelocity;

To:
C#:
                    Vector3 velocity;
                    if (m_NavMeshAgent.updateRotation)
                    {
                        lookRotation = Quaternion.LookRotation(m_NavMeshAgent.desiredVelocity, m_CharacterLocomotion.Up);
                        // The normalized velocity should be relative to the target rotation.
                        velocity = Quaternion.Inverse(lookRotation) * m_NavMeshAgent.desiredVelocity;
                    }
                    else
                    {
                        velocity = m_NavMeshAgent.desiredVelocity;
                    }

strafing can then be enabled by setting the NavMeshAgent.updateRotation to false and re-enabled by setting it to true.
As an alternative you could add this kind of bool statement inside the ability itself.

Otherwise the AI, despite the path, moves straight at the player (in aim direction, while aiming of course), ignoring the NavMeshAgent.Destination entirely.
 
Actually this still had some issues, I had to change the line 79 to:

C#:
velocity = m_Transform.InverseTransformDirection(m_NavMeshAgent.desiredVelocity);

Now it works fine.

full Update() method below:

C#:
        public override void Update()
        {
            m_InputVector = Vector2.zero;
            var lookRotation = Quaternion.LookRotation(m_Transform.forward, m_CharacterLocomotion.Up);
            if (m_NavMeshAgent.isOnOffMeshLink) {
                UpdateOffMeshLink();
            } else {
                // When the path is pending the desired velocity isn't correct. Add a small buffer to ensure the path is valid.
                if (m_NavMeshAgent.pathPending) {
                    m_LastPathPendingFrame = Time.frameCount;
                }
                // Only move if a path exists.
                if (m_NavMeshAgent.desiredVelocity.sqrMagnitude > 0.01f && m_NavMeshAgent.remainingDistance > 0.01f && m_LastPathPendingFrame + 2 < Time.frameCount) {

                    Vector3 velocity;
                    if (m_NavMeshAgent.updateRotation)
                    {
                        lookRotation = Quaternion.LookRotation(m_NavMeshAgent.desiredVelocity, m_CharacterLocomotion.Up);
                        // The normalized velocity should be relative to the target rotation.
                        velocity = Quaternion.Inverse(lookRotation) * m_NavMeshAgent.desiredVelocity;
                    }
                    else
                    {
                        velocity = m_Transform.InverseTransformDirection(m_NavMeshAgent.desiredVelocity);
                    }

                    // Only normalize if the magnitude is greater than 1. This will allow the character to walk.
                    if (velocity.sqrMagnitude > 1) {
                        velocity.Normalize();
                    }
                    m_InputVector.x = velocity.x;
                    m_InputVector.y = velocity.z;
                }
            }
            var rotation = lookRotation * Quaternion.Inverse(m_Transform.rotation);
            m_DeltaRotation.y = Utility.MathUtility.ClampInnerAngle(rotation.eulerAngles.y);

            base.Update();
        }
 
Top