C# 限制玩家上下旋转

C# 限制玩家上下旋转,c#,unity3d,C#,Unity3d,我有基本的运动和旋转,但我不能想出一个方法来限制上下旋转。我想让你的上下角度不能超过90° 我尝试了多种方法,如使用if支架和使用夹具 使用UnityEngine 公共类控制器:单行为{ public float speed = 5f; public float sensitivity = 2f; public GameObject Camera; CharacterController controller; float moveFB; float moveLR; public floa

我有基本的运动和旋转,但我不能想出一个方法来限制上下旋转。我想让你的上下角度不能超过90°

我尝试了多种方法,如使用if支架和使用夹具

使用UnityEngine

公共类控制器:单行为{

public float speed = 5f;
public float sensitivity = 2f;
public GameObject Camera;

CharacterController controller;

float moveFB;
float moveLR;

public float rotX;
public float rotY;



void Start()
{
    controller = GetComponent<CharacterController>();
    Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void FixedUpdate ()
{
    moveFB = Input.GetAxis("Vertical");
    moveLR = Input.GetAxis("Horizontal");

    rotX = Input.GetAxis("Mouse X") * sensitivity;
    rotY = Input.GetAxis("Mouse Y") * sensitivity;


    transform.Rotate(0, rotX, 0);
    Vector3 movement = new Vector3(moveLR * speed * Time.deltaTime, 0, moveFB * speed * Time.deltaTime);
    controller.Move(transform.rotation * movement);
    Camera.transform.Rotate(-rotY, 0, 0);

}
public float speed = 5f;
public float sensitivity = 2f;
public GameObject Camera;

CharacterController controller;

float moveFB;
float moveLR;

public float rotX;
public float rotY;

public float minAngle = -90f;
public float maxAngle = 90f;





void Start()
{
    controller = GetComponent<CharacterController>();
    Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void FixedUpdate ()
{
    moveFB = Input.GetAxis("Vertical");
    moveLR = Input.GetAxis("Horizontal");

    rotX = Input.GetAxis("Mouse X") * sensitivity;
    rotY -= Input.GetAxis("Mouse Y") * sensitivity;
    rotY = Mathf.Clamp(rotY, minAngle, maxAngle);


    transform.Rotate(0, rotX, 0);
    Vector3 movement = new Vector3(moveLR * speed * Time.deltaTime, 0, moveFB * speed * Time.deltaTime);
    controller.Move(transform.rotation * movement);
    Camera.transform.localRotation = Quaternion.Euler(rotY, 0, 0);



}
公共浮动速度=5f;
公众浮动敏感度=2f;
公共游戏机;
字符控制器;
浮动移动fb;
浮动移动;
公共浮动轮换;
公众浮标;
void Start()
{
控制器=GetComponent();
Cursor.lockState=CursorLockMode.Locked;
}
//每帧调用一次更新
无效固定更新()
{
moveFB=Input.GetAxis(“垂直”);
moveLR=Input.GetAxis(“水平”);
rotX=Input.GetAxis(“鼠标X”)*灵敏度;
rotY=Input.GetAxis(“鼠标Y”)*灵敏度;
旋转(0,旋转,0);
Vector3 movement=新的Vector3(moveLR*speed*Time.deltaTime,0,moveFB*speed*Time.deltaTime);
控制器。移动(变换。旋转*移动);
摄影机。变换。旋转(-rotY,0,0);
}
}

使用此代码,您可以将相机旋转90度以上,使其倒置等。

“camera”是一个内置的unity类,我建议将其重命名为“camera”。 尝试此操作以夹紧相机的旋转:
(与其他公共花车一起)

(在FixedUpdate的末尾)

编辑:将eulerAngles更改为localEulerAngles
编辑2:更改了Mathf参数的顺序。DeltaAngle是一个内置的unity类,我建议将其重命名为“Camera”。 尝试此操作以夹紧相机的旋转:
(与其他公共花车一起)

(在FixedUpdate的末尾)

编辑:将eulerAngles更改为localEulerAngles

编辑2:更改了Mathf.DeltaAngle参数的顺序

我在这里修复了它。不确定它是如何工作的,但它是工作的。视频贷记:

使用UnityEngine

公共类控制器:单行为{

public float speed = 5f;
public float sensitivity = 2f;
public GameObject Camera;

CharacterController controller;

float moveFB;
float moveLR;

public float rotX;
public float rotY;



void Start()
{
    controller = GetComponent<CharacterController>();
    Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void FixedUpdate ()
{
    moveFB = Input.GetAxis("Vertical");
    moveLR = Input.GetAxis("Horizontal");

    rotX = Input.GetAxis("Mouse X") * sensitivity;
    rotY = Input.GetAxis("Mouse Y") * sensitivity;


    transform.Rotate(0, rotX, 0);
    Vector3 movement = new Vector3(moveLR * speed * Time.deltaTime, 0, moveFB * speed * Time.deltaTime);
    controller.Move(transform.rotation * movement);
    Camera.transform.Rotate(-rotY, 0, 0);

}
public float speed = 5f;
public float sensitivity = 2f;
public GameObject Camera;

CharacterController controller;

float moveFB;
float moveLR;

public float rotX;
public float rotY;

public float minAngle = -90f;
public float maxAngle = 90f;





void Start()
{
    controller = GetComponent<CharacterController>();
    Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void FixedUpdate ()
{
    moveFB = Input.GetAxis("Vertical");
    moveLR = Input.GetAxis("Horizontal");

    rotX = Input.GetAxis("Mouse X") * sensitivity;
    rotY -= Input.GetAxis("Mouse Y") * sensitivity;
    rotY = Mathf.Clamp(rotY, minAngle, maxAngle);


    transform.Rotate(0, rotX, 0);
    Vector3 movement = new Vector3(moveLR * speed * Time.deltaTime, 0, moveFB * speed * Time.deltaTime);
    controller.Move(transform.rotation * movement);
    Camera.transform.localRotation = Quaternion.Euler(rotY, 0, 0);



}
公共浮动速度=5f;
公众浮动敏感度=2f;
公共游戏机;
字符控制器;
浮动移动fb;
浮动移动;
公共浮动轮换;
公众浮标;
公共浮动米南格尔=-90f;
公共浮动最大角度=90f;
void Start()
{
控制器=GetComponent();
Cursor.lockState=CursorLockMode.Locked;
}
//每帧调用一次更新
无效固定更新()
{
moveFB=Input.GetAxis(“垂直”);
moveLR=Input.GetAxis(“水平”);
rotX=Input.GetAxis(“鼠标X”)*灵敏度;
rotY-=Input.GetAxis(“鼠标Y”)*灵敏度;
rotY=数学夹具(rotY、minAngle、maxAngle);
旋转(0,旋转,0);
Vector3 movement=新的Vector3(moveLR*speed*Time.deltaTime,0,moveFB*speed*Time.deltaTime);
控制器。移动(变换。旋转*移动);
Camera.transform.localRotation=Quaternion.Euler(rotY,0,0);
}

}我把它修好了。不确定它是如何工作的,但它是工作的。视频贷记:

使用UnityEngine

公共类控制器:单行为{

public float speed = 5f;
public float sensitivity = 2f;
public GameObject Camera;

CharacterController controller;

float moveFB;
float moveLR;

public float rotX;
public float rotY;



void Start()
{
    controller = GetComponent<CharacterController>();
    Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void FixedUpdate ()
{
    moveFB = Input.GetAxis("Vertical");
    moveLR = Input.GetAxis("Horizontal");

    rotX = Input.GetAxis("Mouse X") * sensitivity;
    rotY = Input.GetAxis("Mouse Y") * sensitivity;


    transform.Rotate(0, rotX, 0);
    Vector3 movement = new Vector3(moveLR * speed * Time.deltaTime, 0, moveFB * speed * Time.deltaTime);
    controller.Move(transform.rotation * movement);
    Camera.transform.Rotate(-rotY, 0, 0);

}
public float speed = 5f;
public float sensitivity = 2f;
public GameObject Camera;

CharacterController controller;

float moveFB;
float moveLR;

public float rotX;
public float rotY;

public float minAngle = -90f;
public float maxAngle = 90f;





void Start()
{
    controller = GetComponent<CharacterController>();
    Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void FixedUpdate ()
{
    moveFB = Input.GetAxis("Vertical");
    moveLR = Input.GetAxis("Horizontal");

    rotX = Input.GetAxis("Mouse X") * sensitivity;
    rotY -= Input.GetAxis("Mouse Y") * sensitivity;
    rotY = Mathf.Clamp(rotY, minAngle, maxAngle);


    transform.Rotate(0, rotX, 0);
    Vector3 movement = new Vector3(moveLR * speed * Time.deltaTime, 0, moveFB * speed * Time.deltaTime);
    controller.Move(transform.rotation * movement);
    Camera.transform.localRotation = Quaternion.Euler(rotY, 0, 0);



}
公共浮动速度=5f;
公众浮动敏感度=2f;
公共游戏机;
字符控制器;
浮动移动fb;
浮动移动;
公共浮动轮换;
公众浮标;
公共浮动米南格尔=-90f;
公共浮动最大角度=90f;
void Start()
{
控制器=GetComponent();
Cursor.lockState=CursorLockMode.Locked;
}
//每帧调用一次更新
无效固定更新()
{
moveFB=Input.GetAxis(“垂直”);
moveLR=Input.GetAxis(“水平”);
rotX=Input.GetAxis(“鼠标X”)*灵敏度;
rotY-=Input.GetAxis(“鼠标Y”)*灵敏度;
rotY=数学夹具(rotY、minAngle、maxAngle);
旋转(0,旋转,0);
Vector3 movement=新的Vector3(moveLR*speed*Time.deltaTime,0,moveFB*speed*Time.deltaTime);
控制器。移动(变换。旋转*移动);
Camera.transform.localRotation=Quaternion.Euler(rotY,0,0);
}

}

我不知道我是否正确添加了它,但当我尝试它时,当你直视地面或仰望天空时,相机会有点抽搐。糟糕的是,我认为应该是camera.transform.localEulerAngles(用这个替换两个Euler角度的出现),我已经将它更改为local。该代码确实会阻止您将相机旋转90度,但是它会粘在那里并导致其他问题。请使用
Mathf.DeltaAngle(temp.x,0)
替换
temp.x
,我设法修复了它,刚刚发布了代码。非常感谢您对我的帮助和耐心。我不知道我是否正确添加了它,但当我尝试它时,当你一直看向地面或天空时,相机会有点抽搐。我的错,我认为应该是camera.transform.LocalEuleRanges(用这个替换两个Euler角度的出现)我已经把它换成本地的了。该代码确实会阻止您将相机旋转90度,但是它会粘在那里并导致其他问题。请使用
Mathf.DeltaAngle(temp.x,0)
替换
temp.x
,我设法修复了它,刚刚发布了代码。非常感谢你对我的帮助和耐心。