Instead of a separate Transform or GameObject

This seems to work too!

    void ProcessMouseClick(RaycastHit raycastHit, int layerHit)
    {
        /*
        barycentricCoordinate   The barycentric coordinate of the triangle we hit.
        collider                The Collider that was hit.
        distance                The distance from the ray's origin to the impact point.
        lightmapCoord           The uv lightmap coordinate at the impact point.
        normal                  The normal of the surface the ray hit.
        point                   The impact point in world space where the ray hit the collider.
        rigidbody               The Rigidbody of the collider that was hit. If the collider is not attached to a rigidbody then it is null.
        textureCoord            The uv texture coordinate at the collision location.
        textureCoord2           The secondary uv texture coordinate at the impact point.
        transform               The Transform of the rigidbody or collider that was hit.
        triangleIndex           The index of the triangle that was hit.
        */
        if (WASDInput == false)
        {
            switch(layerHit)
            {
                case walkableLayerNumber:
                    navMeshAgent.destination = raycastHit.point;
                    aiCharacterControl.SetTarget(null);
                    break;
                case enemyLayerNumber:
                    aiCharacterControl.SetTarget(raycastHit.transform);
                    break;
                default:
                    aiCharacterControl.SetTarget(null);
                    break;
            }
        }
    }

It seems cleaner. However, it does need this update to make sure the ThirdPersonCharacter’s parameters don’t get changed by the NavMeshAgent:

void ProcessDirectMovement()
    {
        // Read inputs.
        // These are defined under Project Settings > Input.
        hAxis = Input.GetAxis("Horizontal");
        vAxis = Input.GetAxis("Vertical");

        // Calculate camera relative direction to move.
        // Scale multiplies two vectors component-wise.
        // Take cameraForward (the blue Z axis of the camera), X and Z stay same, Y (height) is ignored. 
        // Set vector length to 1 with .normalized (we only need direction, not length). 
        // It makes no difference, but is just a convention showing that we're not going to do anything with the length.
        // Ignoring Y is just for clarity purposes. 
        // It makes no difference, but shows that we're not going to do anything with the y value.
        // Basically we project the blue axis of the camera on the ground this way.
        // Take main.transform.right (the red X of the camera). 
        // This movement direction has no height as it merely indicates the cameras rotation towards the XZ plane (the ground) and not the tilt.
        Vector3 cameraForward = Vector3.Scale(Camera.main.transform.forward, new Vector3(1, 0, 1)).normalized;
        // Take the input "strength" and apply that to the calculated the move direction. 
        // (The further you move, the farther you go).
        Vector3 movement = vAxis * cameraForward + hAxis * Camera.main.transform.right;

        thirdPersonCharacter.m_MoveSpeedMultiplier = 2.5f;
        thirdPersonCharacter.m_AnimSpeedMultiplier = 2.5f;
        aiCharacterControl.SetTarget(player.transform);
        thirdPersonCharacter.Move(movement, false, false);
    }

Privacy & Terms