2017-09-10 2 views
0

Ceci est mon script CamMouseLook et j'en ai besoin quand le joueur bouge la souris tout le long il ne tourne pas à l'envers. Je veux qu'il soit capable de regarder juste pas tellement jusqu'à ce qu'il tourne la vue à l'enversUnity Player Controller - comment faire en sorte qu'il ne bouge pas à l'envers

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

public class CamMouseLook : MonoBehaviour { 

    Vector2 mouseLook; 
    Vector2 smoothV; 
    public float sensitivity = 5.0f; 
    public float smoothing = 2.0f; 

    GameObject character; 

    // Use this for initialization 
    void Start() { 
     character = this.transform.parent.gameObject; 
    } 

    // Update is called once per frame 
    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); 
    } 
} 

Répondre

0

Ce que vous pouvez faire est de bloquer la rotation dans un axe spécifique. Par exemple, pour limiter dans l'axe Y et X de sorte que le joueur ne peut tourner de [-60,60] degrés vous pouvez utiliser:

using System; 
using UnityEngine; 

public class MouseLook : MonoBehaviour 
{ 
    public float mouseSensitivity = 70.0f; 
    public float clampAngle = 60.0f; 

    private float rotY = 0.0f; // rotation around the up/y axis 
    private float rotX = 0.0f; // rotation around the right/x axis 

    void Start() 
    { 
     Vector3 rot = transform.localRotation.eulerAngles; 
     rotY = rot.y; 
     rotX = rot.x; 
    } 

    void Update() 
    { 
     float mouseX = Input.GetAxis("Mouse X"); 
     float mouseY = -Input.GetAxis("Mouse Y"); 

     rotY += mouseX * mouseSensitivity * Time.deltaTime; 
     rotX += mouseY * mouseSensitivity * Time.deltaTime; 

     rotX = Mathf.Clamp(rotX, -clampAngle, clampAngle); 

     Quaternion localRotation = Quaternion.Euler(rotX, rotY, 0.0f); 
     transform.rotation = localRotation; 
    } 
} 

Vous pouvez maintenant adapter ce script pour limiter la rotation dans l'angle et la gamme dont vous avez besoin

+0

Je t'aime. thnx fam –