【发布时间】:2018-07-09 01:55:32
【问题描述】:
我在互联网上查找了类似 ROBLOX 中的相机脚本。但我似乎找不到一个。谁能帮我这个? 顺便说一句,我正在使用 Unity 2017.3
【问题讨论】:
-
这可能会对你有所帮助:(youtube.com/watch?v=AOTEg91eXAc)[RobloxCamera tuts.]
我在互联网上查找了类似 ROBLOX 中的相机脚本。但我似乎找不到一个。谁能帮我这个? 顺便说一句,我正在使用 Unity 2017.3
【问题讨论】:
欢迎来到 Stack Overflow。
我会尽力帮助你。但请记住:
通常,此类问题会收到负面反馈,因为问题不够具体,而且您没有提供任何尝试或代码供我们使用。 人们有时将此视为“你能为我做我的工作”的场景。
你是新人,所以无论如何我都想努力帮助你。 我希望这对您和其他潜在的人有所帮助。 为了清楚起见,我已经包含了代码 cmets。
[编辑]
可能值得一提的是,我还添加了一个暂停功能来解锁光标并将控制权返回给鼠标,这可以根据需要构建或删除。
脚本:
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;
}
}
}
【讨论】: