Search Unity

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

Question How can I make my character taller without breaking my camera?

Discussion in 'Getting Started' started by brygonnn, Oct 24, 2023.

  1. brygonnn

    brygonnn

    Joined:
    Jun 22, 2023
    Posts:
    16
    I used a Youtube tutorial to code a game, but I wish to make my character taller, but doing so makes my main characters camera to start stuttering, I'm not smart when it comes to coding (Barely understand certain concepts) to I'm hoping someone can help explain how to fix it, thank you.

    Code (CSharp):
    1. public class PlayerMotor : MonoBehaviour
    2. {
    3.  
    4.     private CharacterController controller;
    5.     private Vector3 playerVelocity;
    6.     public float speed = 2f;
    7.     private bool isGrounded;
    8.     public float gravity = -9.8f;
    9.     bool lerpCrouch = false;
    10.     bool crouching = false;
    11.     bool sprinting = false;
    12.     float crouchTimer;
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.         controller = GetComponent<CharacterController>();
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.         isGrounded = controller.isGrounded;
    24.         if (lerpCrouch)
    25.         {
    26.             crouchTimer += Time.deltaTime;
    27.             float p = crouchTimer / 1;
    28.             p *= p;
    29.             if (crouching)
    30.                 controller.height = Mathf.Lerp(controller.height, 1, p);
    31.             else
    32.                 controller.height = Mathf.Lerp(controller.height, 2, p);
    33.             if (p > 1)
    34.             {
    35.                 lerpCrouch = false;
    36.                 crouchTimer = 0f;
    37.             }
    38.         }
    39.     }
    40.     public void Crouch()
    41.     {
    42.         crouching = !crouching;
    43.         crouchTimer = 0;
    44.         lerpCrouch = true;
    45.         if (crouching && sprinting)
    46.             speed = 1;
    47.         if (crouching)
    48.             speed = 1;
    49.         else
    50.             speed = 2;
    51.     }
    52.     public void Sprint()
    53.     {
    54.         sprinting = !sprinting;
    55.         if (sprinting)
    56.             speed = 4;
    57.         else
    58.             speed = 2;
    59.         if (crouching && sprinting)
    60.             speed = 1;
    61.     }
    62.     public void ProcessMove(Vector2 input)
    63.     {
    64.         Vector3 moveDirection = Vector3.zero;
    65.         moveDirection.x = input.x;
    66.         moveDirection.z = input.y;
    67.         controller.Move(transform.TransformDirection(moveDirection) * speed * Time.deltaTime);
    68.         playerVelocity.y += gravity * Time.deltaTime;
    69.         if (isGrounded && playerVelocity.y < 0)
    70.             playerVelocity.y = -2f;
    71.         controller.Move(playerVelocity * Time.deltaTime);
    72.         Debug.Log(playerVelocity.y);
    73.     }
    74. }
    75.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerLook : MonoBehaviour
    6. {
    7.  
    8.     public Camera cam;
    9.     private float xRotation = 0f;
    10.     public float xSensitivity = 65f;
    11.     public float ySenitivity = 65f;
    12.  
    13.     public void ProcessLook(Vector2 input)
    14.     {
    15.         float mouseX = input.x;
    16.         float mouseY = input.y;
    17.         xRotation -= (mouseY * Time.deltaTime) * ySenitivity;
    18.         xRotation = Mathf.Clamp(xRotation, -80f, 80f);
    19.         cam.transform.localRotation = Quaternion.Euler(xRotation, 0, 0);
    20.         transform.Rotate(Vector3.up * (mouseX * Time.deltaTime) * xSensitivity);
    21.     }
    22. }
    23.  
    Bonus, I seem to phase through a wall every time I crouch and uncrouch under it, I asked for help but was never able to fix it, maybe you guys can help, thanks again.
     
  2. brygonnn

    brygonnn

    Joined:
    Jun 22, 2023
    Posts:
    16
    Video just in case you needed more info.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,300
    Unfortunately this script is based upon defective sample code still hosted today on Unity's website.

    The "tell" for me is line 67 and line 71 above where
    controller.Move()
    is called... twice.

    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!!

    Not only that, but germane to your original question... pretty sure you can set the camera height however you like in these ones... Give it a try...
     
  4. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    761
    You may be struggling with a weird quirk of a character controller that causes it to jitter when only moving on the Y axis. For example, when moving upwards on a platform or when standing up. I just took a peek into a character controller that I once made that needed a crouch ability. And it seems my solution was to just move the character slightly while standing up.

    Here's the crouching part:

    Code (CSharp):
    1.     void Croucher()
    2.     {
    3.         if (Input.GetKeyDown(KeyCode.Space))  
    4.             headOffset=-headOffset; // Flips between 0.75f and -0.75f
    5.         if (headOffset>0 && cc.height<2f) // wanting to stand up?
    6.         {
    7.             if (Physics.SphereCast(transform.position,0.2f,Vector3.up,out RaycastHit hit,0.9f)) // something above us?
    8.                 return;
    9.             velocity.x+=0.01f; // prevents jitter when standing (don't ask)
    10.         }
    11.         cc.height=Mathf.Lerp(cc.height,1.25f+headOffset,0.1f);
    12.     }
     
    Last edited: Oct 24, 2023
  5. brygonnn

    brygonnn

    Joined:
    Jun 22, 2023
    Posts:
    16
    I just got everything running, but just like my previous code, me uncrouching under an object still cause me to phase through it, what can I do to stop it?
     
  6. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    761
    Only the character controller's x,y and z movement is restricted by collisions. It's height isn't restricted at all. So when your script increases the controller's height it has to prevent the controller's head from penetrating a ceiling or anything above. Check out the example I posted earlier, it uses SphereCast to detect for anything above and prevents the controller from "standing up" if there's anything present.
     
  7. brygonnn

    brygonnn

    Joined:
    Jun 22, 2023
    Posts:
    16
    How would I go on about writing that code in because I used different type of code, take a look
    Code (CSharp):
    1.  }
    2.     public void Crouch()
    3.     {
    4.         crouching = !crouching;
    5.         crouchTimer = 0;
    6.         lerpCrouch = true;
    7.         if (crouching && sprinting)
    8.             speed = 1;
    9.         if (crouching)
    10.             speed = 1;
    11.         else
    12.             speed = 2;
    13.     }
    14.  
     
  8. brygonnn

    brygonnn

    Joined:
    Jun 22, 2023
    Posts:
    16
    Also this part

    Code (CSharp):
    1.  if (lerpCrouch)
    2.         {
    3.             crouchTimer += Time.deltaTime;
    4.             float p = crouchTimer / 1;
    5.             p *= p;
    6.             if (crouching)
    7.                 controller.height = Mathf.Lerp(controller.height, 1, p);
    8.             else
    9.                 controller.height = Mathf.Lerp(controller.height, 2, p);
    10.             if (p > 1)
    11.             {
    12.                 lerpCrouch = false;
    13.                 crouchTimer = 0f;
    14.             }
    15.         }
     
  9. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    761
    Well the height increase happens on line 9 of the lerpCrouch section, so you'll need to insert a test there. That whole crouch code looks very messy and so I'm not touching it. :)
     
    Last edited: Oct 26, 2023