Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

FPS Camera Leaning script

Discussion in 'Scripting' started by btm30x, Jun 30, 2022.

  1. btm30x

    btm30x

    Joined:
    May 12, 2022
    Posts:
    2
    Hey guys I have been working on the player controller for my fps game and I can't get the camera to rotate left or right when I lean. The if statements are all set up and the camera transform.Translate works fine but it refuses to rotate. I have tried Quaternions Eulers whatever I could find on youtube and forum post but nothing work please advise.

    Script:


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

    [RequireComponent(typeof(CharacterController))]

    public class PlayerMovement : MonoBehaviour
    {
    public float walkingSpeed = 7.5f;
    public float runningSpeed = 11.5f;
    public float jumpSpeed = 8.0f;
    public float gravity = 20.0f;

    public Camera playerCamera;
    public Camera managerCamera;
    public GameObject playerCam;
    public Collider PlayerCollider;
    public Collider CrouchCollider;
    public Collider ProneCollider;
    public float lookSpeed = 2.0f;
    public float lookXLimit = 45.0f;

    //Varriables for Crouch and Prone
    private bool IsStanding = true;
    private bool IsCrouched = false;
    private bool IsProne = false;
    public bool HuntMode = true;
    public bool ManageMode = false;

    //Varriables for Peaking
    private bool IsPeakingLeft = false;
    private bool IsPeakingRight = false;
    private bool PeakIdle = true;

    CharacterController characterController;
    Vector3 moveDirection = Vector3.zero;
    float rotationX = 0;

    [HideInInspector]
    public bool canMove = true;

    void Start()
    {
    characterController = GetComponent<CharacterController>();
    Cursor.lockState = CursorLockMode.Locked;
    Cursor.visible = false;

    }

    void Update()
    {

    //Crouch Movement

    if (Input.GetKeyDown(KeyCode.LeftControl)&& IsStanding == true && HuntMode == true)
    {
    PlayerCollider.enabled = false;
    CrouchCollider.enabled = true;
    ProneCollider.enabled = false;

    Vector3 CamMove = new Vector3(0.0f, -0.55f, 0.0f);
    playerCam.transform.Translate(CamMove);

    IsStanding = false;
    IsCrouched = true;
    IsProne = false;

    }
    else if (Input.GetKeyDown(KeyCode.LeftControl)&& IsCrouched == true && HuntMode == true)
    {
    Vector3 CamMove = new Vector3(0.0f, 0.55f, 0.0f);
    playerCam.transform.Translate(CamMove);

    IsCrouched = false;
    IsStanding = true;
    IsProne = false;
    }
    else if (Input.GetKeyDown(KeyCode.LeftControl)&& IsProne == true && HuntMode == true)
    {
    Vector3 CamMove = new Vector3(0.0f, 0.55f, 0.0f);
    playerCam.transform.Translate(CamMove);

    IsCrouched = true;
    IsProne = false;
    IsStanding = false;
    }


    //Prone Movement

    if (Input.GetKeyDown(KeyCode.Z)&& IsStanding == true && HuntMode == true)
    {
    PlayerCollider.enabled = false;
    CrouchCollider.enabled = false;
    ProneCollider.enabled = true;

    Vector3 CamMove = new Vector3(0.0f, -1.1f, 0.0f);
    playerCam.transform.Translate(CamMove);

    IsProne = true;
    IsCrouched = false;
    IsStanding = false;
    }
    else if (Input.GetKeyDown(KeyCode.Z)&& IsCrouched == true && HuntMode == true)
    {
    Vector3 CamMove = new Vector3(0.0f, -0.55f, 0.0f);
    playerCam.transform.Translate(CamMove);

    IsProne = true;
    IsCrouched = false;
    IsStanding = false;
    }
    else if (Input.GetKeyDown(KeyCode.Z)&& IsProne == true && HuntMode == true)
    {
    Vector3 CamMove = new Vector3(0.0f, 1.1f, 0.0f);
    playerCam.transform.Translate(CamMove);
    IsProne = false;
    IsCrouched = false;
    IsStanding = true;
    }




    //Return to standing from Crouched or Prone


    if (Input.GetKeyDown(KeyCode.Space)&& IsCrouched == true && HuntMode == true)
    {
    PlayerCollider.enabled = true;
    CrouchCollider.enabled = false;
    ProneCollider.enabled = false;

    Vector3 CamMove = new Vector3(0.0f, 0.55f, 0.0f);
    playerCam.transform.Translate(CamMove);

    IsCrouched = false;
    IsStanding = true;
    IsProne = false;
    }
    else if (Input.GetKeyDown(KeyCode.Space)&& IsProne == true && HuntMode == true)
    {
    Vector3 CamMove = new Vector3(0.0f, 1.1f, 0.0f);
    playerCam.transform.Translate(CamMove);
    IsProne = false;
    IsCrouched = false;
    IsStanding = true;
    }


    //RTS vs FPS camera switcher

    if (Input.GetKeyDown(KeyCode.M) && HuntMode == true)
    {
    HuntMode = false;
    Cursor.visible = true;
    managerCamera.enabled = true;
    playerCamera.enabled = false;
    ManageMode = true;

    }
    else if (Input.GetKeyDown(KeyCode.M) && HuntMode == false)
    {
    HuntMode = true;
    Cursor.visible = false;
    managerCamera.enabled = false;
    playerCamera.enabled = true;
    ManageMode = false;
    }

    //Lean player left and right

    //Idle to left lean
    if (Input.GetKeyDown(KeyCode.Q) && PeakIdle == true)
    {
    Vector3 CamMove = new Vector3(-0.5f, 0.0f, 0.0f);
    playerCam.transform.Translate(CamMove);

    playerCam.transform.rotation = new Quaternion(-30,0, 0, 0);

    IsPeakingLeft = true;
    IsPeakingRight = false;
    PeakIdle = false;

    }
    //Idle to right lean
    else if (Input.GetKeyDown(KeyCode.E) && PeakIdle == true)
    {
    Vector3 CamMove = new Vector3(0.5f, 0.0f, 0.0f);
    playerCam.transform.Translate(CamMove);
    playerCam.transform.rotation = Quaternion.Euler(new Vector3(0,45,0));

    IsPeakingLeft = false;
    IsPeakingRight = true;
    PeakIdle = false;
    }
    //Left to Idle lean
    else if (Input.GetKeyDown(KeyCode.E) && IsPeakingLeft == true)
    {
    Vector3 CamMove = new Vector3(0.5f, 0.0f, 0.0f);
    playerCam.transform.Translate(CamMove);

    IsPeakingLeft = false;
    IsPeakingRight = false;
    PeakIdle = true;
    }
    //Left to Idle lean #2
    else if (Input.GetKeyDown(KeyCode.Q) && IsPeakingLeft == true)
    {
    Vector3 CamMove = new Vector3(0.5f, 0.0f, 0.0f);
    playerCam.transform.Translate(CamMove);

    IsPeakingLeft = false;
    IsPeakingRight = false;
    PeakIdle = true;
    }
    //Right to Idle lean
    else if (Input.GetKeyDown(KeyCode.Q) && IsPeakingRight == true)
    {
    Vector3 CamMove = new Vector3(-0.5f, 0.0f, 0.0f);
    playerCam.transform.Translate(CamMove);

    IsPeakingLeft = false;
    IsPeakingRight = false;
    PeakIdle = true;
    }
    //Right to Idle lean #2
    else if (Input.GetKeyDown(KeyCode.E) && IsPeakingRight == true)
    {
    Vector3 CamMove = new Vector3(-0.5f, 0.0f, 0.0f);
    playerCam.transform.Translate(CamMove);


    IsPeakingLeft = false;
    IsPeakingRight = false;
    PeakIdle = true;
    }

    // We are grounded, so recalculate move direction based on axes
    Vector3 forward = transform.TransformDirection(Vector3.forward);
    Vector3 right = transform.TransformDirection(Vector3.right);
    // Press Left Shift to run
    bool isRunning = Input.GetKey(KeyCode.LeftShift);
    float curSpeedX = canMove ? (isRunning ? runningSpeed : walkingSpeed) * Input.GetAxis("Vertical") : 0;
    float curSpeedY = canMove ? (isRunning ? runningSpeed : walkingSpeed) * Input.GetAxis("Horizontal") : 0;
    float movementDirectionY = moveDirection.y;
    moveDirection = (forward * curSpeedX) + (right * curSpeedY);



    if (Input.GetButton("Jump") && canMove && characterController.isGrounded && HuntMode == true)
    {
    moveDirection.y = jumpSpeed;
    }
    else
    {
    moveDirection.y = movementDirectionY;
    }

    // Apply gravity. Gravity is multiplied by deltaTime twice (once here, and once below
    // when the moveDirection is multiplied by deltaTime). This is because gravity should be applied
    // as an acceleration (ms^-2)
    if (!characterController.isGrounded && HuntMode == true)
    {
    moveDirection.y -= gravity * Time.deltaTime;
    }

    // Move the controller
    characterController.Move(moveDirection * Time.deltaTime);

    // Player and Camera rotation
    if (canMove && HuntMode == true)
    {
    rotationX += -Input.GetAxis("Mouse Y") * lookSpeed;
    rotationX = Mathf.Clamp(rotationX, -lookXLimit, lookXLimit);
    playerCamera.transform.localRotation = Quaternion.Euler(rotationX, 0, 0);
    transform.rotation *= Quaternion.Euler(0, Input.GetAxis("Mouse X") * lookSpeed, 0);
    }

    }

    }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    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
    - links to documentation you used to cross-check your work (CRITICAL!!!)
    - what actually happened, especially any errors you see

    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/

    You may edit your post above.

    You must find a way to get the information you need in order to reason about what the problem is.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494