Home > Back-end >  Unity GameObject rotating around global axis
Unity GameObject rotating around global axis

Time:01-08

I'm starting to create my first game on unity in 3D and for this I'm consulting many tutorials but I haven't found an answer to my question. I created a script that rotates the main camera when moving the mouse. But the character does not rotate on itself but seems to rotate around an axis external to his body How can I do? Thanks a lot for the replies.

Here the code:

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

public class visual : MonoBehaviour
{
    public Transform player;
    float sensibility = 100f;
    float rotation;

    void Start ()
    {
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
    }

    void Update ()
    {
        float x = Input.GetAxis("Mouse X") * Time.deltaTime * sensibility;
        float y = Input.GetAxis("Mouse Y") * Time.deltaTime * sensibility;

        rotation -= y;
        rotation = Mathf.Clamp(rotation, -60f, 60f);

        transform.localRotation = Quaternion.Euler(rotation, 0, 0); 

        player.Rotate(Vector3.up * x);
    }
}

I tried to search a right code but I didn't find any working code.

CodePudding user response:

Without knowing the setup of you player object this is more a guess: Check the player object whether the visuals are not in a child with an offset for example. Or as derHugo mentioned the offset could be wrong.

Alternatively you might wanna check on enter image description here

  • Related