Ressources

Catégorie scripts.

MouseCamLook.cs (Génération Procédurale - Épisode 13)

Script utilisé dans l'épisode 13 de la série génération procédurale (Basé sur le repo suivant : https://github.com/jiankaiwang/FirstPersonController)
Copier
		 								
		 									
using UnityEngine;

public class MouseCamLook : MonoBehaviour
{
    public GameObject character;

    [SerializeField]
    public float sensitivity = 5.0f;
    private float smoothing = 2.0f;
    
    private Vector2 mouseLook;
    private Vector2 smoothV;

    void Update()
    {
        var md = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
        md = Vector2.Scale(md, new Vector2(sensitivity * smoothing, sensitivity * smoothing));
        smoothV.x = Mathf.Lerp(smoothV.x, md.x, 1f / smoothing);
        smoothV.y = Mathf.Lerp(smoothV.y, md.y, 1f / smoothing);
        mouseLook += smoothV;

        transform.localRotation = Quaternion.AngleAxis(-mouseLook.y, Vector3.right);
        character.transform.localRotation = Quaternion.AngleAxis(mouseLook.x, character.transform.up);
    }
}