Search code examples
c#unity-game-enginegame-development

Camera Changing Position When I Hit Play


so, I am trying to implement a FPS model and the camera is on that model's eye level.

Model is based on multiple objects in a hierarchy ( I have removed all the colliders from child objects) The model has character control.

The camera is the child of that Model.

I have lined up everything correctly. If I manually rotate Model, Camera will rotate accordingly and all if fine.

But when I hit play right as the simulation starts camera goes to rotation(0,0,0)

I don't understand what is happening. I tried to manually face the camera at the start method to Model's rotation. but that does not work.

Before Play https://i.sstatic.net/eKW8G.jpg

After Play https://i.sstatic.net/0ReY7.jpg

Mouse Look Script

public class mouselook : MonoBehaviour
{
    public float mousesens = 100f;
    public Transform playerBody;
    float xRotation = 0f;
    // Start is called before the first frame update
    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
        // transform.rotation = playerBody.rotation;
    }

    // Update is called once per frame
    void Update()
    {
        float mouseX = Input.GetAxis("Mouse X") * mousesens * Time.deltaTime;
        float mouseY = Input.GetAxis("Mouse Y") * mousesens * Time.deltaTime;
        xRotation -= mouseY;
        xRotation = Mathf.Clamp(xRotation, -90f, 90f);
        transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
        // transform.Rotate(Vector3.up * mouseX);

        playerBody.Rotate(Vector3.up * mouseX);




    }
}

Movement Script (IDK if it is relevent to this problem)

public class movement : MonoBehaviour
{
    public CharacterController controller;
    public float speed = 12f;
    public float gravity = -9.81f;
    public Transform groundCheck;
    public float groundDistance = 0.4f;
    public float jumpHeight = 3f;

    public LayerMask groundMask;
    Vector3 velocity;
    bool isGrounded;
    // 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 * -2f * gravity);
        }
        velocity.y += gravity * Time.deltaTime;
        controller.Move(velocity * Time.deltaTime);
    }
}

I suspect, this is going to some easy answer, which i have wasted hours on.

Problem is With this line, in mouse look script

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

Solution

  • Had to set x-axis to a different value. and it works perfectly, did some changing in the movement script, changed some directions.

    Mouse script

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

    Movement Script

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

    Thank you all.