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.
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 Spaces . By default when you call transform.Rotate
Space is set to Space.Self
but maybe switching it to Space.World
and then see what happens could at least give you a further hint.
Update after looking into linked code:
The orange markes object has the script that rotates with the camera. The yellow marked objects ( Main Camera
and Player
) must be centered within the player
object.
How to solve this? Set the Transform->Position
for Main Camera
and Player
to 0,0,0
. Then adjust the ONLY height (so y
) of the Main Camera
and Player
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.