Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
472 views
in Technique[技术] by (71.8m points)

c# - Unity 3D - Limit Camera Rotation

I have found several awnsers to this question online, and I have tried all of them, but they either break my camera, or just overall don't work.

Here is my script:

using UnityEngine;
using System.Collections;

public class fp : MonoBehaviour
{

public float speedH = 2.0f;
public float speedV = 2.0f;

private float yaw = 0.0f;
private float pitch = 0.0f;

void Update()
{
    yaw += speedH * Input.GetAxis("Mouse X");
    pitch -= speedV * Input.GetAxis("Mouse Y");

    transform.eulerAngles = new Vector3(pitch, yaw, 0.0f);
}
}

As far as I know, there is 3 solutions to this problem, but I don't know how to implement any of the solutions

Solution 1: Convert the script above to Unityscript (I have little expirience with C#) and I can solve the problem with "if" statements.

Solution 2: Provide the C# code to limit the angle on my script to an angle of 90 degrees all axis

Solution 3: All of the above

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You don't post what you've tried, so this is a shot in the dark on helping you. Check Unity's Mathf.Clamp to restrict the angles allowed.

yaw += speedH * Input.GetAxis("Mouse X");
pitch -= speedV * Input.GetAxis("Mouse Y");

yaw = Mathf.Clamp(yaw, -90f, 90f);
pitch = Mathf.Clamp(pitch, -60f, 90f);

transform.eulerAngles = new Vector3(pitch, yaw, 0.0f);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...