[SOLVED] Need help with First Person Controller script.

I’ve been learning to use Unity/C# for a few months now (used to make games with Flash/AS3) so there is still a ton I have to learn.

In my current Unity game (Unity 5) I am using the First Person Controller. I understand enough of its code to manipulate it to make it behave the way I want but there are also other aspects of it that are beyond my current programming knowledge.

In the First Person Controller script there is this line:

m_IsWalking = !Input.GetKey(KeyCode.LeftShift);

As I am sure you guys know it checks if the Left Shift key is pressed and if not the character walks, otherwise the character runs.

I recently added gamepad support for my game and everything works great. Almost. I needed to modify the line above so that my character will run when the left trigger on the gamepad is pressed so I modified the line like so:

m_IsWalking = Input.GetAxis("Sprint") == 0;

I added a Sprint in the Input Manager for the left trigger on the gamepad. This works great as intended.

The issue I am trying to figure out with my current method is that I can only use one of the two lines I listed above. This means I’m currently forced either have the player sprint with the keyboard or with the controller. I can’t do both.

I initially tried:

m_IsWalking = !Input.GetKey(KeyCode.LeftShift) || Input.GetAxis("Sprint") == 0;

But that stupidly failed because I didn’t realize the First Person Controller variable m_IsWalking is a boolean when I first made this attempt.

Anyone here familiar enough with the code in the First Person Controller script that knows of a way to achieve what I want? I’m really hoping there is a simple solution I am missing that wouldn’t require me to mess with the script too much given there is a lot in there that just kind of goes over my head.

Wouldn’t it be an AND? The character is walking if left shift isn’t pressed and Sprint axis is 0.

m)Is_Walking = !Input.Getkey(KeyCode.LeftShift) && Input.GetAxis("Sprint") == 0;
1 Like

It would be if it was contained in an if statement. This is why I was hoping to find someone that’s manipulated the First Person Controller script before. You can find the m_IsWalking variable and how it is setup on line 215 of the First Person Controller if you’re interested.

I stand corrected. I couldn’t reproduce the error for some reason. The is in fact no error at all this time. Your solution works. Thank you for taking the time to help me I really appreciate it :slight_smile:

1 Like