0
\$\begingroup\$

I am making a simple parkour game in which the camera will follow the mouse based on the mouse input axis * the sensitivity. This works great, but when I want to reset my character to the start I cant figure out how to reset the mouse axis to zero, thus preventing me from resetting the camera rotation. I have tried Input.ResetInputAxes();, but that seems to only reset the buttons.

using System.Collections;
using System;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{

    public CharacterController characterController;
    public Transform Player, camera;
    public Camera cam;
    public float XSensitivity, YSensitivity;
    public bool grounded, jumping;
    public float forwardSpeed, horizontalSpeed, jumpPower, gravity, sprintModifier;

    [HideInInspector] public float sideSpeed, lastPlayerPosX, lastPlayerPosZ, jumpSpeed, verticalRotation, horizontalRotation;

    private float isSprinting = 1;

    void Update()
    {
        //This is where I get the axes
        float mouseX = Input.GetAxis("Mouse X"), mouseY = Input.GetAxis("Mouse Y");

        //This is how I rotate the player and camera 
        verticalRotation += -mouseY * YSensitivity;
        Player.transform.localRotation = Quaternion.Euler(0, horizontalRotation, 0);
        horizontalRotation += mouseX * XSensitivity;
        cam.transform.localRotation = Quaternion.Euler(verticalRotation, horizontalRotation, 0);

        if (Input.GetMouseButtonDown(0))
        {
            Cursor.lockState = CursorLockMode.Locked;
        }

        float horizontal = Input.GetAxis("Horizontal"), forward = Input.GetAxis("Vertical");

        grounded = characterController.isGrounded;

        if (grounded)
        {
            if (Input.GetKey("left shift"))
            {
                isSprinting = sprintModifier;
            }
            if (Input.GetKey("left shift") == false)
            {
                isSprinting = 1f;
            }
            jumping = false;
            jumpSpeed = 0;
            sideSpeed = .75f;
        }
        else
            jumpSpeed -= (gravity * 25) * Time.deltaTime;

        if (Input.GetKey("space"))
        {
            sideSpeed = .25f;
        }
        if (Input.GetAxisRaw("Jump") != 0 && !jumping)
        {
            jumping = true;
            jumpSpeed = jumpPower;
        }

        lastPlayerPosX = Player.transform.position.x;

        lastPlayerPosZ = Player.transform.position.z;

        Vector3 motion = new Vector3(horizontal * horizontalSpeed * sideSpeed, jumpSpeed, forward * forwardSpeed * isSprinting);
        characterController.Move((Player.transform.rotation * motion) * Time.deltaTime);
    }

    void OnTriggerEnter(Collider col)
    {
        if (col.gameObject.name == "deathbarrier")
        {
            //Resets player to starting position
            Player.transform.position = new Vector3(20.5f, -2, -22.5f);
            //Is supposed to reset all inputs (including mouse)
            Input.ResetInputAxes();
            //Sets camera rotation to 0, but only for one frame before the stored mouse axes change it
            camera.transform.rotation = Quaternion.Euler(0, 0, 0);
        }
    }
}

Thanks for any tips!

\$\endgroup\$
1
\$\begingroup\$

You don't need to reset the mouse axes.

The mouse axes (e.g. Input.GetAxis("Mouse X")) reflect mouse movement over the last frame. If you do not move the mouse, the value returned from this axis will be 0.

You need to reset your fields horizontalRotation and verticalRotation in your OnTriggerEnter().

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.