0
\$\begingroup\$

I need to use Left-Shift to run, but I can't figure out how to do so.

Here my code:

using UnityEngine;
using System.Collections;

// This script moves the character controller forward
// and sideways based on the arrow keys.
// It also jumps when pressing space.
// Make sure to attach a character controller to the same game object.
// It is recommended that you make only one call to Move or SimpleMove per frame.

public class Move : MonoBehaviour
{
    CharacterController characterController;

    public float speed = 6.0f;
    public float jumpSpeed = 8.0f;
    public float gravity = 20.0f;
    public Transform charBody;
    private Vector3 moveDirection = Vector3.zero;
    public float mouseSense = 11f;
    public float runSpeed = 12f;
    public bool isRunning = false;

    void Start()
    {
        characterController = GetComponent<CharacterController>();
    }

    void Update()
    {
        if (characterController.isGrounded)
        {
            // We are grounded, so recalculate
            // move direction directly from axes

            moveDirection = transform.forward * Input.GetAxis("Horizontal") + transform.right * Input.GetAxis("Vertical");
            moveDirection *= speed;
            moveDirection = transform.right * Input.GetAxis("Horizontal") + transform.right * Input.GetAxis("Vertical");
            moveDirection *= speed;





            if (Input.GetButton("Jump"))
            {
                moveDirection.y = jumpSpeed;
            }
        }
           
        

        // Apply gravity. Gravity is multiplied by deltaTime twice (once here, and once below
        // when the moveDirection is multiplied by deltaTime). This is because gravity should be applied
        // as an acceleration (ms^-2)
        moveDirection.y -= gravity * Time.deltaTime;

        // Move the controller
        characterController.Move(moveDirection * Time.deltaTime);

        float MOUSEY = Input.GetAxis("Mouse Y") * mouseSense * Time.deltaTime;
        float MOUSEX = Input.GetAxis("Mouse X") * mouseSense * Time.deltaTime;

        charBody.Rotate(Vector3.up * MOUSEX);

        


    }
}

When Left-Shift is pressed I want the speed to change to runSpeed.

\$\endgroup\$
1

2 Answers 2

2
\$\begingroup\$

First I would rename the variable speed to walkSpeed to better differentiate it from runSpeed. Then I would create a local variable speed in Update and assign it based on the current state of the shift key:

float speed;
if (Input.GetKey(KeyCode.LeftShift)) {
    speed = runSpeed;
} else {
    speed = walkSpeed;
}

This architecture also allows to take other circumstantial modifiers to the character speed into account.

Then I would use that local speed variable to calculate the moveDirection:

Vector3 input = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical");
moveDirection = transform.right * input * runSpeed;

moveDirection.y -= gravity * Time.deltaTime;

characterController.Move(moveDirection * Time.deltaTime);
\$\endgroup\$
0
\$\begingroup\$

Answer:

using UnityEngine;
using System.Collections;

// This script moves the character controller forward
// and sideways based on the arrow keys.
// It also jumps when pressing space.
// Make sure to attach a character controller to the same game object.
// It is recommended that you make only one call to Move or SimpleMove per frame.

public class Move : MonoBehaviour
{
    CharacterController characterController;

    public float speed = 6.0f;
    public float jumpSpeed = 8.0f;
    public float gravity = 20.0f;
    public Transform charBody;
    private Vector3 moveDirection = Vector3.zero;
    public float mouseSense = 11f;
    public float runSpeed = 12f;
    public bool isRunning = false;
    float camRot = 0f;
    public Transform altCamera;

    void Start()
    {
        characterController = GetComponent<CharacterController>();
    }

    void Update()
    {
        if (characterController.isGrounded)
        {
            // We are grounded, so recalculate
            // move direction directly from axes

            moveDirection = transform.forward * Input.GetAxis("Horizontal") + transform.right * Input.GetAxis("Vertical");
            moveDirection *= speed;
            moveDirection = transform.right * Input.GetAxis("Horizontal") + transform.right * Input.GetAxis("Vertical");
            moveDirection *= speed;





            if (Input.GetButton("Jump"))
            {
                moveDirection.y = jumpSpeed;
            }

            if (Input.GetKey(KeyCode.LeftShift))
            {
                moveDirection = transform.forward * Input.GetAxis("Horizontal") + transform.right * Input.GetAxis("Vertical");
                moveDirection *= runSpeed;
                moveDirection = transform.right * Input.GetAxis("Horizontal") + transform.right * Input.GetAxis("Vertical");
                moveDirection *= runSpeed;

            }
        }
           
        

        // Apply gravity. Gravity is multiplied by deltaTime twice (once here, and once below
        // when the moveDirection is multiplied by deltaTime). This is because gravity should be applied
        // as an acceleration (ms^-2)
        moveDirection.y -= gravity * Time.deltaTime;

        // Move the controller
        characterController.Move(moveDirection * Time.deltaTime);

        float MOUSEY = Input.GetAxis("Mouse Y") * mouseSense * Time.deltaTime;
        float MOUSEX = Input.GetAxis("Mouse X") * mouseSense * Time.deltaTime;



        charBody.Rotate(Vector3.up * MOUSEX);

        



        


    }
}
```
\$\endgroup\$
4
  • \$\begingroup\$ Your solution makes the character move twice per update. One time with the regular speed and then again with the enhanced run speed. Are you sure that's what you want? \$\endgroup\$
    – Philipp
    May 21, 2021 at 11:21
  • \$\begingroup\$ Are you sure? It's not doing so for me.... \$\endgroup\$ May 21, 2021 at 11:23
  • \$\begingroup\$ Ah OK, I misread that the moveDirection gets set twice but only gets applied to the characterController once. Still pretty wasteful to first calculate the moveDirection according to the regular speed, and then when shift is pressed discard that calculation and repeat it with runSpeed instead of Speed. \$\endgroup\$
    – Philipp
    May 21, 2021 at 11:29
  • \$\begingroup\$ Wait, you are calculating and discarding the moveDirection twice. You are first calculating it with transform.forward and then you overwrite that result with another value calculated with transform.right. In both the regular speed and the runSpeed calculation. Why? \$\endgroup\$
    – Philipp
    May 21, 2021 at 11:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .