参考书籍:Unity3D动作游戏开发实战
常规第三人称相机是指相机在玩家身后以一定距离看向玩家,相机位置可受到鼠标移动或者手柄摇杆操作的控制而左右上下旋转。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EasyThirdPersonCamera : MonoBehaviour
{
Vector3 mDefaultDir; //默认方向
Transform mPlayerTransform; //玩家的Transform
public float distance = 4f; //相机观测巨鹿
public float speed = 120f; //相机旋转速度
public Vector3 offset = new Vector3(0,1.5f,0); //观测目标偏移值
private void OnEnable()
{
const string PLAYER = "Player";
var upAxis = -Physics.gravity.normalized;
//玩家变换
mPlayerTransform = GameObject.FindGameObjectWithTag(PLAYER).transform;
mDefaultDir = Vector3.ProjectOnPlane((transform.position - mPlayerTransform.position),upAxis).normalized;
}
private void Update()
{
//计算偏移后的玩家位置
var from = mPlayerTransform.localToWorldMatrix.MultiplyPoint3x4(offset);
var to = from + mDefaultDir * distance; //相机位置
transform.position = to;//ObstacleProcess(from,to); //相机位置赋值
transform.LookAt(from); //相机旋转锁定
}
}
先贴上代码,回头继续补充。。。