Character Run Speed Stacking?

I’ve looked through the internet and it says I have to Normalize the Magnitude however when I placed that in my script it just moved very slowly I was wondering if you could help real quick and explain why

P.S. I know i’ve been asking a lot of questions but I’m new to writing code so please bare with me :slight_smile:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(CharacterController))]
public class FPSController : MonoBehaviour
{
    public Camera playerCamera;
    public float walkSpeed = 20f;
    public float runSpeed = 12f;
    public float jumpPower = 7f;
    public float gravity = 10f;


    public float lookSpeed = 2f;
    public float lookXLimit = 20f;


    Vector3 moveDirection = Vector3.zero;
    float rotationX = 0;

    public bool canMove = true;


    CharacterController characterController;
    void Start()
    {
        characterController = GetComponent<CharacterController>();
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
    }

    void Update()
    {

        #region Handles Movment
        Vector3 forward = transform.TransformDirection(Vector3.forward).normalized;
        Vector3 right = transform.TransformDirection(Vector3.right).normalized;

        // Press Left Shift to run
        bool isRunning = Input.GetKey(KeyCode.LeftShift);
        float curSpeedX = canMove ? (isRunning ? runSpeed : walkSpeed) * Input.GetAxis("Vertical") : 0;
        float curSpeedY = canMove ? (isRunning ? runSpeed : walkSpeed) * Input.GetAxis("Horizontal") : 0;
        float movementDirectionY = moveDirection.y;
        moveDirection = (forward * curSpeedX) + (right * curSpeedY);
        #endregion

        #region Handles Jumping
        if (Input.GetButton("Jump") && canMove && characterController.isGrounded)
        {
            moveDirection.y = jumpPower;
        }
        else
        {
            moveDirection.y = movementDirectionY;
        }

        if (!characterController.isGrounded)
        {
            moveDirection.y -= gravity * Time.deltaTime;
        }

        #endregion

        #region Handles Rotation
        characterController.Move(moveDirection * Time.deltaTime);

        if (canMove)
        {
            rotationX += -Input.GetAxis("Mouse Y") * lookSpeed;
            rotationX = Mathf.Clamp(rotationX, -lookXLimit, lookXLimit);
            playerCamera.transform.localRotation = Quaternion.Euler(rotationX, 0, 0);
            transform.rotation *= Quaternion.Euler(0, Input.GetAxis("Mouse X") * lookSpeed, 0);
        }

        #endregion
    }
}

I know my Movement and Look Rotation Are handled in the same script.

I have no idea what “Character Run Speed Stacking” is.

Normalizing a vector has a very specific mathematical meaning, completely unrelated to Unity.

Normalizing a vector makes the magnitude of the vector be 1.0f. That’s all.

If you’re just implementing run vs walk, there’s ten billion tutorials and example code blobs out there. Here’s one:

The basics are something like:

  • assume a walk speed by setting the chosenSpeed to walk speed
  • check if RUN is in effect, if so, set the chosenSpeed to run speed
  • any other things like “extra boots of speed” added onto chosenSpeed

Now use chosenSpeed to perform the movement.

Otherwise, if you’d care to clarify further:

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

This is the bare minimum of information to report:

  • what you want
  • what you tried
  • what you expected to happen
  • what actually happened, log output, variable values, and especially any errors you see
  • links to documentation you used to cross-check your work (CRITICAL!!!)

The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven’t put effort into finding the documentation, why should we bother putting effort into replying?

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: Using code tags properly

  • Do not TALK about code without posting it.
  • Do NOT post unformatted code.
  • Do NOT retype code. Use copy/paste properly using code tags.
  • Do NOT post screenshots of code.
  • Do NOT post photographs of code.
  • Do NOT attach entire scripts to your post.
  • ONLY post the relevant code, and then refer to it in your discussion.

You was normalizing the forward and right vectors which are already normalized. I suspect you was trying to normalize the move direction but that’s not a good idea because then you’ll lose the slightly smoothed input that you get from GetAxis.

Here’s a very basic controller:

using UnityEngine;
public class FPSController : MonoBehaviour
{
CharacterController cc;
float gravity=10;
public float walkSpeed=10f;
public bool canMove=true;
public float lookSpeed=2f;
public float lookXLimit=70;
float rotationX=0;
   void Start()
   {
      cc=GetComponent<CharacterController>();
      Cursor.lockState = CursorLockMode.Locked; // this locks and hides the mouse pointer
   }

   void Update()
   {
      Vector3 moveDirection=Vector3.zero;
      if (canMove)
      {
         // look around:
         rotationX += -Input.GetAxis("Mouse Y") * lookSpeed;
         rotationX = Mathf.Clamp(rotationX, -lookXLimit, lookXLimit);
         Camera.main.transform.localRotation = Quaternion.Euler(rotationX, 0, 0);
         transform.rotation *= Quaternion.Euler(0, Input.GetAxis("Mouse X") * lookSpeed, 0);
         // move around:
         moveDirection=(transform.right*Input.GetAxis("Horizontal"))+(transform.forward*Input.GetAxis("Vertical"));
         moveDirection=Vector3.ClampMagnitude(moveDirection,1f)*walkSpeed;  // this prevents the character from being able to move faster in the diagonal direction
         if (Input.GetKey(KeyCode.LeftShift)) // run?
            moveDirection*=2f;   // double the speed
         if (cc.isGrounded && Input.GetButton("Jump"))
            moveDirection.y=cc.velocity.y+5;
      }
      if (!cc.isGrounded)
         moveDirection.y=cc.velocity.y-(gravity*Time.deltaTime);
      cc.Move(moveDirection*Time.deltaTime);
   }
}