【unity小技巧】FPS游戏实现相机的偏移震动、武器射击后退和后坐力效果

最终效果

在这里插入图片描述

前言

关于后坐力之前其实已经分享了一个:FPS游戏后坐力制作思路

但是实现起来比较复杂,如果你只是想要简单的实现,可以看看这个,其实原理是控制相机的震动实现后坐力和偏移

相机偏移震动

相机震动脚本

新增CameraOffset,Singleton是一个泛型单例

// 控制相机偏移
public class CameraOffset : Singleton<CameraOffset>
{
    
    
    private Vector3 currentRotation; // 当前旋转角度
    private Vector3 targetRotation; // 目标旋转角度
    public float snappiness; // 旋转平滑度
    public float returnAmount; // 回归平滑度

    void Update()
    {
    
    
        // 让目标旋转角度逐渐回归到零向量
        targetRotation = Vector3.Lerp(targetRotation, Vector3.zero, Time.deltaTime * returnAmount);

        // 使用插值方法让当前旋转角度逐渐接近目标旋转角度
        currentRotation = Vector3.Slerp(currentRotation, targetRotation, Time.fixedDeltaTime * snappiness);

        // 将当前旋转角度应用到相机的局部旋转
        transform.localRotation = Quaternion.Euler(currentRotation);
    }

    // 震动相机
    public void Shake(int i)
    {
    
    
        // 设置目标旋转角度为一个在(-i, i)范围内的随机向量
        targetRotation = new Vector3(Random.Range(-i, i), Random.Range(-i, i), Random.Range(-i, i));
    }
}

配置参数,脚本挂载在相机身上
在这里插入图片描述
注意,通常相机的偏移震动可能会和人物鼠标控制相机视角冲突,所以最好是给相机新增一个父类,防止二者互相干扰
在这里插入图片描述

换弹节点震动

还可以在武器换弹时的某些节点,调用相机的震动,实现不错的人物操作反馈
在这里插入图片描述

//动画事件
public class PangXieAnimEnvent : MonoBehaviour
{
    
    
    public void PangXieAnim(int i)
    {
    
    
        CameraOffset.Instance.Shake(i);
    }
}

挂载PangXieAnimEnvent 脚本到对应带Animator的武器身上
在这里插入图片描述

武器射击后退效果

//枪支向后移动
public class WeaponBack : Singleton<WeaponBack> 
{
    
    
    private Vector3 startPosition;
    public float aimSmoothing = 10;//平滑度
    private void Start() {
    
    
        startPosition = transform.localPosition;
    }
    private void Update() {
    
    
        Vector3 desiredPosition = Vector3.Lerp(transform.localPosition, startPosition, Time.deltaTime * aimSmoothing); // 使用插值平滑过渡到目标位置

        transform.localPosition = desiredPosition; // 更新枪支的本地位置
    }

    public void Back(){
    
    
        transform.localPosition -= Vector3.forward * 0.1f; // 使枪支向后移动
    }
}

调用·

WeaponBack.Instance.Back();

武器后坐力效果

这里再分享一个实现武器后坐力脚本

// 武器后坐力脚本
public class WeaponRecoil : Singleton<WeaponRecoil>
{
    
    
    public float minX, maxX; // 后坐力产生的旋转角度范围
    public float minY, maxY;
    public Transform recoilCamera; // 产生后坐力的相机

    Vector3 rot; // 相机当前的旋转角度

    private void Update()
    {
    
    
        rot = recoilCamera.transform.localRotation.eulerAngles;
        if (rot.x != 0 || rot.y != 0)
        {
    
    
            // 如果相机的旋转角度不为0,则逐渐回到0的旋转角度
            recoilCamera.transform.localRotation = Quaternion.Slerp(recoilCamera.transform.localRotation, Quaternion.Euler(0, 0, 0), Time.deltaTime * 3);
        }
    }

    // 产生后坐力效果的方法
    public void Recoil()
    {
    
    
        // 在规定的范围内随机产生旋转角度偏移量
        float recX = Random.Range(minX, maxX);
        float recY = Random.Range(minY, maxY);

        // 将相机的旋转角度设置为产生偏移后的角度,并更新到相机上
        recoilCamera.transform.localRotation = Quaternion.Euler(rot.x - recY, rot.y + recX, rot.z);
    }
}

在武器射击时调用后坐力

WeaponRecoil.Instance.Recoil();

配置参数
在这里插入图片描述
效果
在这里插入图片描述

完结

赠人玫瑰,手有余香!如果文章内容对你有所帮助,请不要吝啬你的点赞评论和关注,以便我第一时间收到反馈,你的每一次支持都是我不断创作的最大动力。当然如果你发现了文章中存在错误或者有更好的解决方法,也欢迎评论私信告诉我哦!

好了,我是向宇https://xiangyu.blog.csdn.net

一位在小公司默默奋斗的开发者,出于兴趣爱好,最近开始自学unity,闲暇之余,边学习边记录分享,站在巨人的肩膀上,通过学习前辈们的经验总是会给我很多帮助和启发!php是工作,unity是生活!如果你遇到任何问题,也欢迎你评论私信找我, 虽然有些问题我也不一定会,但是我会查阅各方资料,争取给出最好的建议,希望可以帮助更多想学编程的人,共勉~

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_36303853/article/details/135419290