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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

crouching scripts

Discussion in 'Scripting' started by Eucalyp, Feb 25, 2020.

  1. Eucalyp

    Eucalyp

    Joined:
    Nov 4, 2019
    Posts:
    8
    Hi, i'm doing my first FPS game in unity and I'm looking for a way to make my character crouch, I'm using FPS controller from brackeys's tutorial and there is no clear tutorial or anything on that. I basically just want the hitbox and the character's height to decrease and the camera to follow, but I don't know how to do that. Can Someone help please?

    here are my scripts:

    movement script:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class playerMovement : MonoBehaviour
    {
    public CharacterController controller;
    public Camera camera;

    public float speed = 12f;

    public float gravity = -9.81f;

    public float jumpHeight = 3f;

    Vector3 velocity;
    bool isGrounded;

    public Transform groundCheck;
    public float groundDistance = 0.5f;
    public LayerMask groundMask;


    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
    isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

    if (isGrounded && velocity.y < 0)
    {
    velocity.y = -2f;
    }

    float x = Input.GetAxis("Horizontal");

    float z = Input.GetAxis("Vertical");

    Vector3 move = transform.right * x + transform.forward * z;

    controller.Move(move * speed * Time.deltaTime);

    if (Input.GetButtonDown("Jump") && isGrounded)
    {
    velocity.y = Mathf.Sqrt(jumpHeight * -2 * gravity);
    }

    if (Input.GetButton("Crouch"))
    {
    controller.height = 0.6f;
    print("grebfhsijndalk");
    }
    else
    {
    controller.height = 1f;
    }

    velocity.y += gravity * Time.deltaTime;

    controller.Move(velocity * Time.deltaTime);
    }

    void Crouch()
    {
    controller.height = 0.6f;
    }
    }


    fp look

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

    public class mouseLook : MonoBehaviour
    {
    public float mouseSensitivity = 200f;

    public Transform playerBody;

    float xRotation = 0f;

    // Start is called before the first frame update
    void Start()
    {
    Cursor.lockState = CursorLockMode.Locked;
    }

    // Update is called once per frame
    void Update()
    {
    float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
    float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;

    playerBody.Rotate(Vector3.up * mouseX);

    xRotation -= mouseY;
    xRotation = Mathf.Clamp(xRotation, -90f, 90f);

    transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
    }
    }

     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,745
    In the Learning section of Unity website there's Beginners section. The basic Animator tutorial with Robot Kyle running around directly answers your question.
     
    APSchmidtOfOld likes this.
  3. Eucalyp

    Eucalyp

    Joined:
    Nov 4, 2019
    Posts:
    8
    I can't find the beginners section. can you send a link please?
     
  4. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,745
  5. Eucalyp

    Eucalyp

    Joined:
    Nov 4, 2019
    Posts:
    8
    thank you, but the camera is a third person camera, my main problem is that my FPS camera doesn't goes done as I crouch.
     
  6. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,745
    well, then lower your camera like the video did with collider
     
    eses likes this.
  7. Eucalyp

    Eucalyp

    Joined:
    Nov 4, 2019
    Posts:
    8
    nvm, i fixed everything, thanks for your help!