许可优化
产品
解决方案
服务支持
关于
软件库
当前位置:服务支持 >  软件文章 >  unity3d FPS 枪的后座力

unity3d FPS 枪的后座力

阅读数 13
点赞 0
article_banner

实现枪开枪后, 向上偏移一段距离,再缓慢下移复位(模仿cs)

这里写图片描述

调小后座力

这里写图片描述

using UnityEngine;
using System.Collections;

public class Camera2Follower : MonoBehaviour {
    // 枪cd计时器
    float timer;

    // 后座力 之前枪摄像头的角度
    Vector3 s_pre_euler;
    public float gun_end_force = 0.53f; // 枪后座力大小 (可以先调大些方便调试)
    void Update ()
    {
         ....
         timer += Time.deltaTime;
          if (Input.GetButton("Fire1") && timer >= 0.15)
          {
              // 计时器清零
              timer = 0f;
              s_pre_euler = transform.eulerAngles;
              // 后座力
              rotationY += Input.GetAxis("Mouse Y") * sensitivityY + gun_end_force;
              Quaternion yQuaternion2 = Quaternion.AngleAxis(rotationY, Vector3.left);
              transform.localRotation = originalRotation * yQuaternion2;

          }

          // 检测鼠标有没有移动
          if (Input.GetAxis("Mouse Y") != 0)
          {
              //Debug.Log("X: " + transform.eulerAngles);
              s_pre_euler = transform.eulerAngles;
              rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
              rotationY = ClampAngle(rotationY, minimumY, maximumY);

              Quaternion yQuaternion = Quaternion.AngleAxis(rotationY, Vector3.left);
              transform.localRotation = originalRotation * yQuaternion;
          }
          else
          {
              recoverGun();
          }
    }

    // 恢复后座力以前的位置
    void recoverGun() {
        s_pre_euler.y = transform.eulerAngles.y;
        Quaternion current_cam = Quaternion.Euler(transform.eulerAngles);
        Quaternion target_cam = Quaternion.Euler(s_pre_euler);
        transform.eulerAngles = Quaternion.Slerp(current_cam, target_cam, 5 * Time.deltaTime).eulerAngles;
    }

    void Start ()
    {
        s_pre_euler = transform.eulerAngles;
    }

    // 限制角度
    public static float ClampAngle (float angle, float min, float max)
    {
        if (angle < -360F)
            angle += 360F;
        if (angle > 360F)
            angle -= 360F;
        return Mathf.Clamp (angle, min, max);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66

看了一下cs的枪后座力 发现还有左右抖动,于是又加了点代码

y_angle_mat = xxxxxxxx;    // 上下方向
....
// 在枪开火的方法区加
float xAngle = Random.Range(0.0f,1.0f);
Quaternion x_angle_mat = Quaternion.AngleAxis(xAngle, Vector3.up);
transform.localRotation = originalRotation * y_angle_mat * x_angle_mat;                  // 相当于乘以一个矩阵
c#
运行
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删
相关文章
QR Code
微信扫一扫,欢迎咨询~

联系我们
武汉格发信息技术有限公司
湖北省武汉市经开区科技园西路6号103孵化器
电话:155-2731-8020 座机:027-59821821
邮件:tanzw@gofarlic.com
Copyright © 2023 Gofarsoft Co.,Ltd. 保留所有权利
遇到许可问题?该如何解决!?
评估许可证实际采购量? 
不清楚软件许可证使用数据? 
收到软件厂商律师函!?  
想要少购买点许可证,节省费用? 
收到软件厂商侵权通告!?  
有正版license,但许可证不够用,需要新购? 
联系方式 155-2731-8020
预留信息,一起解决您的问题
* 姓名:
* 手机:

* 公司名称:

姓名不为空

手机不正确

公司不为空