Search Unity

Trying to setup a good crouching mechanism

Discussion in 'Scripting' started by keenanwoodall, Dec 18, 2014.

  1. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    598
    So right now everything works great, except that every once in a while the player teleports to 0 on the y axis. It's because I'm setting the position of the player. That's the problem, but I don't know how to fix it without messing up my crouching mechanic. Here's my code:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.    
    8.     //Public Variables
    9.         //Movement Variables
    10.     public float moveSpeed = 3.0f;
    11.     public float sprintSpeed = 6.0f;
    12.     public float crouchSpeed = 1.5f;
    13.     public float crouchHeight = 1.0f;
    14.     public float crouchTransitionSpeed = 5.0f;
    15.     public float crouchingJumpPower = 0.0f;
    16.     public float jumpPower = 7.5f;
    17.     public float wallJumpPushOffPower = 3.0f;
    18.     public float wallJumpPushUpPower = 7.5f;
    19.     public float minWallCollisionAngle = 60.0f;
    20.     public float gravity = -20.0f;
    21.         //Input Variabes
    22.     public string moveAxisX = "Horizontal";
    23.     public string moveAxisZ = "Vertical";
    24.     public string sprintButton = "Sprint";
    25.     public string crouchButton = "Crouch";
    26.     public string jumpButton = "Jump";
    27.     //Hidden Variabes
    28.    
    29.     //Private Variables
    30.         //Movement Variables
    31.     private Vector3 movement;
    32.     private bool crouching;
    33.         //Component Variables
    34.     private CharacterController controller;
    35.  
    36.     void Start ()
    37.     {
    38.         //We have to tell the controller variable what CharacterController we want to be changing.
    39.         //In this case it's the CharacterController on the object that this script is attached to.
    40.         controller = this.GetComponent<CharacterController>();
    41.     }
    42.     void Update ()
    43.     {
    44.         //We are calling the PlayerMovement method; it holds all the movement functionality for organization.
    45.         Movement ();
    46.     }
    47.     void OnControllerColliderHit (ControllerColliderHit collision)
    48.     {
    49.         if(!controller.isGrounded)
    50.         {
    51.             if(Vector3.Angle(this.transform.forward, collision.normal) > minWallCollisionAngle)
    52.                 if(Input.GetButtonDown(jumpButton))
    53.                     WallJump(collision.normal);
    54.         }
    55.     }
    56.     void Movement ()
    57.     {
    58.         //If the player is touching the ground he will be able to move and jump
    59.         if(controller.isGrounded)
    60.         {
    61.             movement = new Vector3(Input.GetAxis(moveAxisX), 0, Input.GetAxis(moveAxisZ));
    62.             //We have to turn the movement vector into world space instead of local.
    63.             movement = transform.TransformDirection(movement);
    64.             //If the player is holding down the sprint button we'll multiply movement by sprintSpeed.
    65.             if(Input.GetButton(sprintButton))
    66.             {
    67.                 movement *= sprintSpeed;
    68.                 crouching = false;
    69.             }
    70.             //If the player isn't holding the sprint button but IS holding the crouch button we'll multiply movement by crouchSpeed.
    71.             else if(Input.GetButton (crouchButton))
    72.             {
    73.                 movement *= crouchSpeed;
    74.                 controller.height = Mathf.Lerp(controller.height, crouchHeight, crouchTransitionSpeed * Time.deltaTime);
    75.                 this.transform.position = new Vector3(this.transform.position.x, controller.height / 2, this.transform.position.z);
    76.                 crouching = true;
    77.             }
    78.             //Otherwise we'll multiply movement by moveSpeed because the player isn't sprinting.
    79.             else
    80.             {
    81.                 movement *= moveSpeed;
    82.                 controller.height = Mathf.Lerp(controller.height, 2, crouchTransitionSpeed * Time.deltaTime);
    83.                 this.transform.position = new Vector3(this.transform.position.x, controller.height / 2, this.transform.position.z);
    84.                 crouching = false;
    85.             }
    86.             //If the player presses the jump button we'll set the players y movement to the value of jumpPower.
    87.             if(Input.GetButtonDown(jumpButton))
    88.             {
    89.                 if(!crouching)
    90.                     movement.y = jumpPower;
    91.                 else if(crouching)
    92.                     movement.y = crouchingJumpPower;
    93.             }
    94.         }
    95.         //Whether or not the player is touching the ground we'll still move the player down at the speed of the gravity variable.
    96.         movement.y += gravity * Time.deltaTime;
    97.         //By this point the movement variable is calculated correctly and we can now move the player in its direction.
    98.         controller.Move(movement * Time.deltaTime);
    99.     }
    100.     void WallJump (Vector3 normal)
    101.     {
    102.         movement = Vector3.Reflect(transform.forward, normal);
    103.         movement *= wallJumpPushOffPower;
    104.         movement.y = wallJumpPushUpPower;
    105.     }
    106. }