技术难点:由于是第三人称射击游戏,角色和相机之间有夹角,所以枪口点和准星是有误差的,下面是和平精英手游截图,我用AK射击zhuzi
cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFollow : MonoBehaviour
{
public Transform ShootPoint;
//射击距离(米)
public float AimDistance = 100;
public float CamToAimDis = 103;
private Vector3 AimPoint;
//横向观察夹角
public float HorizonAngle = 0.5f;
public float VerticalAngle;
//旋转轴向
public Vector3 axis = new Vector3(0, 1, 0);
void Update()
{
//射击方向的平面方向
Vector3 dir = ShootPoint.forward;
//计算射击目标点
AimPoint = ShootPoint.position + ShootPoint.forward * AimDistance;
Vector3 camDir = Quaternion.AngleAxis(HorizonAngle, axis) * dir;
Vector3 CamPos = AimPoint - Quaternion.AngleAxis(HorizonAngle, axis) * dir * CamToAimDis;
transform.position = CamPos;
transform.forward = camDir;
}
}