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

最终效果

文章目录

前言

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

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

相机偏移震动

相机震动脚本

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

csharp 复制代码
// 控制相机偏移
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));
    }
}

配置参数,脚本挂载在相机身上

注意,通常相机的偏移震动可能会和人物鼠标控制相机视角冲突,所以最好是给相机新增一个父类,防止二者互相干扰

换弹节点震动

还可以在武器换弹时的某些节点,调用相机的震动,实现不错的人物操作反馈

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

挂载PangXieAnimEnvent 脚本到对应带Animator的武器身上

武器射击后退效果

csharp 复制代码
//枪支向后移动
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; // 使枪支向后移动
    }
}

调用·

csharp 复制代码
WeaponBack.Instance.Back();

武器后坐力效果

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

csharp 复制代码
// 武器后坐力脚本
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);
    }
}

在武器射击时调用后坐力

csharp 复制代码
WeaponRecoil.Instance.Recoil();

配置参数

效果

完结

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

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

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

相关推荐
Alfred king4 小时前
面试150 生命游戏
leetcode·游戏·面试·数组
Thomas_YXQ6 小时前
Unity3D DOTS场景流式加载技术
java·开发语言·unity
KhalilRuan10 小时前
Unity-MMORPG内容笔记-其一
unity·游戏引擎
向宇it14 小时前
【unity游戏开发——网络】网络游戏通信方案——强联网游戏(Socket长连接)、 弱联网游戏(HTTP短连接)
网络·http·游戏·unity·c#·编辑器·游戏引擎
qq_1682789519 小时前
Protobuf在游戏开发中的应用:TypeScript + Golang 实践
服务器·golang·游戏引擎
ii_best20 小时前
按键精灵 安卓脚本开发:游戏实战之自动切换账号辅助工具
游戏
切韵10 天前
Unity编辑器扩展:UI绑定复制工具
ui·unity·编辑器
Alfred king10 天前
面试150跳跃游戏
python·leetcode·游戏·贪心算法
D1555408805811 天前
游戏护航小程序源码游戏派单小程序搭建游戏代练小程序源码
游戏
lingling00911 天前
2025 年焊接相机十大品牌测评:抗光耐高温解决方案深度解析
数码相机