Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Character Run Speed Stacking?

Discussion in 'Scripting' started by TWJBS, Dec 1, 2023.

  1. TWJBS

    TWJBS

    Joined:
    May 10, 2023
    Posts:
    5
    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 :)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [RequireComponent(typeof(CharacterController))]
    6. public class FPSController : MonoBehaviour
    7. {
    8.     public Camera playerCamera;
    9.     public float walkSpeed = 20f;
    10.     public float runSpeed = 12f;
    11.     public float jumpPower = 7f;
    12.     public float gravity = 10f;
    13.  
    14.  
    15.     public float lookSpeed = 2f;
    16.     public float lookXLimit = 20f;
    17.  
    18.  
    19.     Vector3 moveDirection = Vector3.zero;
    20.     float rotationX = 0;
    21.  
    22.     public bool canMove = true;
    23.  
    24.  
    25.     CharacterController characterController;
    26.     void Start()
    27.     {
    28.         characterController = GetComponent<CharacterController>();
    29.         Cursor.lockState = CursorLockMode.Locked;
    30.         Cursor.visible = false;
    31.     }
    32.  
    33.     void Update()
    34.     {
    35.  
    36.         #region Handles Movment
    37.         Vector3 forward = transform.TransformDirection(Vector3.forward).normalized;
    38.         Vector3 right = transform.TransformDirection(Vector3.right).normalized;
    39.  
    40.         // Press Left Shift to run
    41.         bool isRunning = Input.GetKey(KeyCode.LeftShift);
    42.         float curSpeedX = canMove ? (isRunning ? runSpeed : walkSpeed) * Input.GetAxis("Vertical") : 0;
    43.         float curSpeedY = canMove ? (isRunning ? runSpeed : walkSpeed) * Input.GetAxis("Horizontal") : 0;
    44.         float movementDirectionY = moveDirection.y;
    45.         moveDirection = (forward * curSpeedX) + (right * curSpeedY);
    46.         #endregion
    47.  
    48.         #region Handles Jumping
    49.         if (Input.GetButton("Jump") && canMove && characterController.isGrounded)
    50.         {
    51.             moveDirection.y = jumpPower;
    52.         }
    53.         else
    54.         {
    55.             moveDirection.y = movementDirectionY;
    56.         }
    57.  
    58.         if (!characterController.isGrounded)
    59.         {
    60.             moveDirection.y -= gravity * Time.deltaTime;
    61.         }
    62.  
    63.         #endregion
    64.  
    65.         #region Handles Rotation
    66.         characterController.Move(moveDirection * Time.deltaTime);
    67.  
    68.         if (canMove)
    69.         {
    70.             rotationX += -Input.GetAxis("Mouse Y") * lookSpeed;
    71.             rotationX = Mathf.Clamp(rotationX, -lookXLimit, lookXLimit);
    72.             playerCamera.transform.localRotation = Quaternion.Euler(rotationX, 0, 0);
    73.             transform.rotation *= Quaternion.Euler(0, Input.GetAxis("Mouse X") * lookSpeed, 0);
    74.         }
    75.  
    76.         #endregion
    77.     }
    78. }
    I know my Movement and Look Rotation Are handled in the same script.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,238
    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:

    https://forum.unity.com/threads/a-basic-first-person-character-controller-for-prototyping.1169491/



    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: https://forum.unity.com/threads/using-code-tags-properly.143875/

    - 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.
     
  3. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    756
    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:
    Code (CSharp):
    1. using UnityEngine;
    2. public class FPSController : MonoBehaviour
    3. {
    4. CharacterController cc;
    5. float gravity=10;
    6. public float walkSpeed=10f;
    7. public bool canMove=true;
    8. public float lookSpeed=2f;
    9. public float lookXLimit=70;
    10. float rotationX=0;
    11.    void Start()
    12.    {
    13.       cc=GetComponent<CharacterController>();
    14.       Cursor.lockState = CursorLockMode.Locked; // this locks and hides the mouse pointer
    15.    }
    16.  
    17.    void Update()
    18.    {
    19.       Vector3 moveDirection=Vector3.zero;
    20.       if (canMove)
    21.       {
    22.          // look around:
    23.          rotationX += -Input.GetAxis("Mouse Y") * lookSpeed;
    24.          rotationX = Mathf.Clamp(rotationX, -lookXLimit, lookXLimit);
    25.          Camera.main.transform.localRotation = Quaternion.Euler(rotationX, 0, 0);
    26.          transform.rotation *= Quaternion.Euler(0, Input.GetAxis("Mouse X") * lookSpeed, 0);
    27.          // move around:
    28.          moveDirection=(transform.right*Input.GetAxis("Horizontal"))+(transform.forward*Input.GetAxis("Vertical"));
    29.          moveDirection=Vector3.ClampMagnitude(moveDirection,1f)*walkSpeed;  // this prevents the character from being able to move faster in the diagonal direction
    30.          if (Input.GetKey(KeyCode.LeftShift)) // run?
    31.             moveDirection*=2f;   // double the speed
    32.          if (cc.isGrounded && Input.GetButton("Jump"))
    33.             moveDirection.y=cc.velocity.y+5;
    34.       }
    35.       if (!cc.isGrounded)
    36.          moveDirection.y=cc.velocity.y-(gravity*Time.deltaTime);
    37.       cc.Move(moveDirection*Time.deltaTime);
    38.    }
    39. }
    40.