Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question CharacterController docs move part needed?

Discussion in 'Scripting' started by Bongmo, May 14, 2023.

  1. Bongmo

    Bongmo

    Joined:
    Jan 18, 2014
    Posts:
    155
    Hi,

    I've used this official CharacterController script:
    https://docs.unity3d.com/2023.2/Documentation/ScriptReference/CharacterController.Move.html

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Example : MonoBehaviour
    6. {
    7.     private CharacterController controller;
    8.     private Vector3 playerVelocity;
    9.     private bool groundedPlayer;
    10.     private float playerSpeed = 2.0f;
    11.     private float jumpHeight = 1.0f;
    12.     private float gravityValue = -9.81f;
    13.  
    14.     private void Start()
    15.     {
    16.         controller = gameObject.AddComponent<CharacterController>();
    17.     }
    18.  
    19.     void Update()
    20.     {
    21.         groundedPlayer = controller.isGrounded;
    22.         if (groundedPlayer && playerVelocity.y < 0)
    23.         {
    24.             playerVelocity.y = 0f;
    25.         }
    26.  
    27.         Vector3 move = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    28.         controller.Move(move * Time.deltaTime * playerSpeed);
    29.  
    30.         if (move != Vector3.zero)
    31.         {
    32.             gameObject.transform.forward = move;
    33.         }
    34.  
    35.         // Changes the height position of the player..
    36.         if (Input.GetButtonDown("Jump") && groundedPlayer)
    37.         {
    38.             playerVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue);
    39.         }
    40.  
    41.         playerVelocity.y += gravityValue * Time.deltaTime;
    42.         controller.Move(playerVelocity * Time.deltaTime);
    43.     }
    44. }
    I commented these lines (30 - 33)
    Code (CSharp):
    1. /*
    2.         if (move != Vector3.zero)
    3.         {
    4.             gameObject.transform.forward = move;
    5.         }
    6. */
    The code still works.

    My question:
    Do I need these lines?

    Could it be that the script was updated and someone forgot to delete that part?
     
  2. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,503
    That depends on whether or not you want the object to face the direction it's moving in. If you do, then yes, you need those lines or equivalent functionality somewhere else.

    If not, then no, you should remove them.

    It's likely there quite deliverately, showing that position and orientation need to each be handled.
     
    Bongmo likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    I suggest discarding the above script because it is derived from faulty example code from Unity.

    I wrote about this before: the Unity example code in the API no longer jumps reliably.

    If you call .Move() twice in one single frame, the grounded check may fail.

    I reported it to Unity via their docs feedback in October 2020. Apparently it is still broken:

    https://docs.unity3d.com/ScriptReference/CharacterController.Move.html

    Here is a work-around:

    https://forum.unity.com/threads/how...racter-movement-in-unity.981939/#post-6379746

    I recommend you also go to that same documentation page and ALSO report that the code is broken.

    When you report it, you are welcome to link the above workaround. One day the docs might get fixed.

    If you would prefer something more full-featured here is a super-basic starter prototype FPS based on Character Controller (BasicFPCC):

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

    That one has run, walk, jump, slide, crouch... it's crazy-nutty!!
     
    angrypenguin likes this.
  4. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,503
    Done. Good chance that it's actually a bug, but either way it needs attention internally.
     
    MaskedMouse and Kurt-Dekker like this.