Unity 中的第三人称相机
第三人称摄像机 是一种放置在玩家身后的摄像机,通常稍微移至一侧,提供游戏级别和玩家本身的视觉表示。
为了在 Unity 中制作第三人称射击游戏 (TPS) 摄像机,我们将结合使用常规玩家移动和第三人称视角。
第 1 步:创建播放器控制器
首先,我们将创建一个处理旋转和移动的玩家控制器:
- 创建一个新的游戏对象(游戏对象 -> 创建空)并命名 "Player"
- 创建一个新的胶囊(游戏对象 -> 3D 对象 -> 胶囊)并将其移动到 "Player" 对象内
- 从 Capsule 中移除 Capsule Collider 组件,并将其位置更改为 (0, 1, 0)
- 创建一个新的 GameObject 并将其命名为 "CameraParent" 并将其移动到 "Player" 对象内,将其位置更改为 (0, 1.64, 0)
- 将主摄像机移动到 "CameraParent" 对象内并将其移动到播放器后面(在我的例子中,我将其移动到这个位置:(0.5, 0.6, -2.9))
- 创建 一个新脚本,将其命名为 SC_TPSController,并将以下代码粘贴到其中:
SC_TPSController.cs
using UnityEngine;
[RequireComponent(typeof(CharacterController))]
public class SC_TPSController : MonoBehaviour
{
public float speed = 7.5f;
public float jumpSpeed = 8.0f;
public float gravity = 20.0f;
public Transform playerCameraParent;
public float lookSpeed = 2.0f;
public float lookXLimit = 60.0f;
CharacterController characterController;
Vector3 moveDirection = Vector3.zero;
Vector2 rotation = Vector2.zero;
[HideInInspector]
public bool canMove = true;
void Start()
{
characterController = GetComponent<CharacterController>();
rotation.y = transform.eulerAngles.y;
}
void Update()
{
if (characterController.isGrounded)
{
// We are grounded, so recalculate move direction based on axes
Vector3 forward = transform.TransformDirection(Vector3.forward);
Vector3 right = transform.TransformDirection(Vector3.right);
float curSpeedX = canMove ? speed * Input.GetAxis("Vertical") : 0;
float curSpeedY = canMove ? speed * Input.GetAxis("Horizontal") : 0;
moveDirection = (forward * curSpeedX) + (right * curSpeedY);
if (Input.GetButton("Jump") && canMove)
{
moveDirection.y = jumpSpeed;
}
}
// Apply gravity. Gravity is multiplied by deltaTime twice (once here, and once below
// when the moveDirection is multiplied by deltaTime). This is because gravity should be applied
// as an acceleration (ms^-2)
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
characterController.Move(moveDirection * Time.deltaTime);
// Player and Camera rotation
if (canMove)
{
rotation.y += Input.GetAxis("Mouse X") * lookSpeed;
rotation.x += -Input.GetAxis("Mouse Y") * lookSpeed;
rotation.x = Mathf.Clamp(rotation.x, -lookXLimit, lookXLimit);
playerCameraParent.localRotation = Quaternion.Euler(rotation.x, 0, 0);
transform.eulerAngles = new Vector2(0, rotation.y);
}
}
}
- 将 SC_TPSController 脚本附加到 "Player" 对象(您会注意到它还添加了另一个名为“字符控制器”的组件。将其中心值更改为 (0, 1, 0))
- 将 "CameraParent" 对象分配给 "Player Camera Parent" 变量
第2步:添加摄像头碰撞检测
Camera 碰撞检测将包含一个脚本,该脚本将检查相机和播放器之间是否有任何东西,并自动将相机移近,从而防止相机剪切对象。
- 创建一个新脚本,将其命名为 SC_CameraCollision,然后将以下代码粘贴到其中:
SC_CameraCollision.cs
using UnityEngine;
public class SC_CameraCollision : MonoBehaviour
{
public Transform referenceTransform;
public float collisionOffset = 0.3f; //To prevent Camera from clipping through Objects
public float cameraSpeed = 15f; //How fast the Camera should snap into position if there are no obstacles
Vector3 defaultPos;
Vector3 directionNormalized;
Transform parentTransform;
float defaultDistance;
// Start is called before the first frame update
void Start()
{
defaultPos = transform.localPosition;
directionNormalized = defaultPos.normalized;
parentTransform = transform.parent;
defaultDistance = Vector3.Distance(defaultPos, Vector3.zero);
//Lock cursor
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
// LateUpdate is called after Update
void LateUpdate()
{
Vector3 currentPos = defaultPos;
RaycastHit hit;
Vector3 dirTmp = parentTransform.TransformPoint(defaultPos) - referenceTransform.position;
if (Physics.SphereCast(referenceTransform.position, collisionOffset, dirTmp, out hit, defaultDistance))
{
currentPos = (directionNormalized * (hit.distance - collisionOffset));
transform.localPosition = currentPos;
}
else
{
transform.localPosition = Vector3.Lerp(transform.localPosition, currentPos, Time.deltaTime * cameraSpeed);
}
}
}
- 将 SC_CameraCollision 脚本附加到主相机
- 将 "CameraParent" 对象分配给 "Reference Transform" 变量
- 调整 "Collision Offset" 和 "Camera Speed" 值,以防相机穿过墙壁
TPS 相机现已准备就绪,请按“播放”进行测试。