C# 类似ROBLOX的摄像头控制C
我已经在互联网上找到了一个类似ROBLOX中的镜头脚本。但我好像找不到。有人能帮我吗?C# 类似ROBLOX的摄像头控制C,c#,unity3d,C#,Unity3d,我已经在互联网上找到了一个类似ROBLOX中的镜头脚本。但我好像找不到。有人能帮我吗? 顺便说一句,我使用的是Unity 2017.3,欢迎使用Stack Overflow 我会尽力帮你的。但请记住: 一般来说,像这样的问题会收到负面反馈,因为问题不够具体,而且您没有提供任何尝试或代码供我们使用。 人们有时认为这是一个“你能为我做我的工作”的场景 你是新来的,所以不管怎样,我都想尝试帮助你。 我希望这对你和其他人都有帮助。 为了清晰起见,我加入了代码注释 要使用它,你必须 在unity C中创建
顺便说一句,我使用的是Unity 2017.3,欢迎使用Stack Overflow 我会尽力帮你的。但请记住: 一般来说,像这样的问题会收到负面反馈,因为问题不够具体,而且您没有提供任何尝试或代码供我们使用。 人们有时认为这是一个“你能为我做我的工作”的场景 你是新来的,所以不管怎样,我都想尝试帮助你。 我希望这对你和其他人都有帮助。 为了清晰起见,我加入了代码注释 要使用它,你必须 在unity C中创建一个新脚本,并将其附加到“玩家”游戏对象 编辑脚本,并将下面的代码粘贴到编辑器中 保存脚本并返回编辑器,脚本将显示新设置 根据您的喜好修改设置,并将相机拖动到“相机”字段中 [编辑] 可能值得一提的是,我还添加了一个暂停功能来解锁光标并将控制返回到鼠标,这可以根据需要构建或删除 剧本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ThirdPersonCamera : MonoBehaviour {
//define some constants
private const float LOW_LIMIT = 0.0f;
private const float HIGH_LIMIT = 85.0f;
//these will be available in the editor
public GameObject theCamera;
public float followDistance = 5.0f;
public float mouseSensitivityX = 4.0f;
public float mouseSensitivityY = 2.0f;
public float heightOffset = 0.5f;
//private variables are hidden in editor
private bool isPaused = false;
// Use this for initialization
void Start () {
//place the camera and set the forward vector to match player
theCamera.transform.forward = gameObject.transform.forward;
//hide the cursor and lock the cursor to center
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update () {
//if escape key (default) is pressed, pause the game (feel free to change this)
if (Input.GetButton("Cancel"))
{
//flip the isPaused state, hide/unhide the cursor, flip the lock state
isPaused = !isPaused;
Cursor.visible = !Cursor.visible;
Cursor.lockState = Cursor.lockState == CursorLockMode.Locked ?
CursorLockMode.None : CursorLockMode.Locked;
System.Threading.Thread.Sleep(200);
}
if(!isPaused)
{
//if we are not paused, get the mouse movement and adjust the camera
//position and rotation to reflect this movement around player
Vector2 cameraMovement = new Vector2(Input.GetAxis("Mouse X"),Input.GetAxis("Mouse Y"));
//first we place the camera at the position of the player + height offset
theCamera.transform.position = gameObject.transform.position + new Vector3(0,heightOffset,0);
//next we adjust the rotation based on the captured mouse movement
//we clamp the pitch (X angle) of the camera to avoid flipping
//we also adjust the values to account for mouse sensitivity settings
theCamera.transform.eulerAngles = new Vector3(
Mathf.Clamp(theCamera.transform.eulerAngles.x + cameraMovement.y * mouseSensitivityY, LOW_LIMIT, HIGH_LIMIT),
theCamera.transform.eulerAngles.y + cameraMovement.x * mouseSensitivityX, 0);
//then we move out to the desired follow distance
theCamera.transform.position -= theCamera.transform.forward * followDistance;
}
}
}
这可能是非活动的,但右键单击会移动:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerCamera : MonoBehaviour
{
private const float LOW_LIMIT = 0.0f;
private const float HIGH_LIMIT = 85.0f;
public GameObject theCamera;
public float followDistance = 5.0f;
public float mouseSensitivityX = 4.0f;
public float mouseSensitivityY = 2.0f;
public float heightOffset = 0.5f;
void Start()
{
theCamera.transform.forward = gameObject.transform.forward;
Cursor.visible = true;
Cursor.lockState = CursorLockMode.None;
}
void Update()
{
if (Input.GetMouseButton(1))
{
Vector2 cameraMovement = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));
theCamera.transform.position = gameObject.transform.position + new Vector3(0, heightOffset, 0);
theCamera.transform.eulerAngles = new Vector3(
Mathf.Clamp(theCamera.transform.eulerAngles.x + cameraMovement.y * mouseSensitivityY, LOW_LIMIT, HIGH_LIMIT),
theCamera.transform.eulerAngles.y + cameraMovement.x * mouseSensitivityX, 0);
theCamera.transform.position -= theCamera.transform.forward * followDistance;
}
}
}
这可能会对你有所帮助:相机啧啧声。]