Walking?

I thought it would look nice if the Enemy walked whilst patrolling. Currently I’m doing this with two new fields, “walkingSpeed” and “runningSpeed”, which I set directly in the Update() each frame via navMeshAgent.speed depending upon whether PatrolBehaviour() is called or not.

Will we encounter walking in a later tutorial or should I stick to my custom implementation?

1 Like

I was just going to do the same thing, and since I see you never got a reply, I’m going to go ahead an implement now…

This will be covered in a later lecture, but it’s fairly close to what’s described here.

1 Like

I literally was just coming back to this post to say that same thing as I am on that video right now… will be interesting to see if my solution was similar…

I was wondering the same thing and got a nice result, using NavMeshAgent (I don’t know why is not displayed in the comment below)

Added in PatrolBehaviour()
GetComponent().speed = patrolSpeed;

and in AttackBehaviour()
GetComponent().speed = agroSpeed;

It’s not displayed because the software thinks it’s a malformed html tag. Try selecting the code and pressing the button marked </>, or if you’re doing a block of code, you can press the backwards ’ key (next to the 1 on your keyboard, the one you shift to get a ~) three times and press enter. This will put you in “code” mode. Type the same backwards single quote three times again to exit code mode.

Not in Code Mode:
GetComponent().speed = patrollspeed;
In Code Mode:

GetComponent<NavMeshAgent>().speed = patrolspeed;
1 Like

I too felt the patrolling was a bit too energetic, so I made a simple implementation by adding some fields on the Mover component along with some methods.

    [SerializeField] float walkSpeed;
    [SerializeField] float runSpeed;

    public void Run()
    {
        navMeshAgent.speed = runSpeed;
    }

    public void Walk()
    {
        navMeshAgent.speed = walkSpeed;
    }

Then called from the AttackBehaviour / GuardBehaviour methods:

mover.Run(); and mover.Walk();

I used this very early on and it has not caused any hiccups.

    if (Input.GetKey(KeyCode.LeftShift))
     { GetComponent<NavMeshAgent>().speed = 3.4f; }
     else { GetComponent<NavMeshAgent>().speed = 1.8f; }

FUNCTION : When player holds the left shift key, when clicking on terrain or enemy the player will accelerate to a defined speed. If the left shift key is released then the player’s speed returns to its initial value;

Stick those few lines (or a facsimile that you modify) at the beginning of PlayerController.cs / Void Update() just after checking if the player is dead but BEFORE calling the mover and fighter functions.

It is a VERY basic feature but it gives me the walk or run option plus can be expanded into its own method later for polishes down the road.

Privacy & Terms