C# 编码平滑射击反冲
所以,我开始遵循一个教程,然后添加了一些我需要的其他东西,一切都很好,甚至反冲,但问题是它真的很起伏,每帧移动一次,一点都不平滑(这是我想要的)我对编程不太了解,所以我希望你能帮助我:) 我的代码:C# 编码平滑射击反冲,c#,unity3d,C#,Unity3d,所以,我开始遵循一个教程,然后添加了一些我需要的其他东西,一切都很好,甚至反冲,但问题是它真的很起伏,每帧移动一次,一点都不平滑(这是我想要的)我对编程不太了解,所以我希望你能帮助我:) 我的代码: using System.Collections; using UnityEngine; using UnityEngine.UI; using UnityEngine.Audio; public class GunController : MonoBeha
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Audio;
public class GunController : MonoBehaviour
{
[Header("Gun Setting")]
public float fireRate = 0.1f;
public int clipSize = 30;
public int reservedAmmoCapacity = 270;
//Variables that change throughout the code
bool canShoot;
int _currentAmmoInClip;
int _ammoInReserve;
//Muzzle Flash
public ParticleSystem muzzleFlash;
//Aiming
public Vector3 normalLocalPosition;
public Vector3 aimingLocalPosition;
public float aimSmoothing = 10;
[Header("Mouse Settings")]
public float mouseSensitivity = 1;
Vector2 _currentRotation;
public float weaponSwayAmount = 10;
//Weapon Recoil
public bool randomizeRecoil;
public Vector2 randomRecoilConstraints;
//You only need to assign if randomize recoil is off
public Vector2[] recoilPattern;
//Audio
AudioSource shootingSound;
//Reloading
public float reloadTime = 1.5f;
private void Start()
{
_currentAmmoInClip = clipSize;
_ammoInReserve = reservedAmmoCapacity;
canShoot = true;
shootingSound = GetComponent<AudioSource>();
}
private void Update()
{
DetermineAim();
DetermineRotation();
if (Input.GetMouseButton(0) && canShoot && _currentAmmoInClip > 0)
{
shootingSound.Play();
StartCoroutine(FinishShooting());
muzzleFlash.Play();
canShoot = false;
_currentAmmoInClip--;
StartCoroutine(ShootGun());
}
else if (Input.GetKeyDown(KeyCode.R) && _currentAmmoInClip < clipSize && _ammoInReserve > 0)
{
StartCoroutine(Reload());
}
}
void DetermineRotation()
{
Vector2 mouseAxis = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
mouseAxis *= mouseSensitivity;
_currentRotation += mouseAxis;
_currentRotation.y = Mathf.Clamp(_currentRotation.y, -90, 90);
transform.localPosition += (Vector3)mouseAxis * weaponSwayAmount / 1000;
transform.root.localRotation = Quaternion.AngleAxis(_currentRotation.x, Vector3.up);
transform.parent.localRotation = Quaternion.AngleAxis(-_currentRotation.y, Vector3.right);
}
void DetermineAim()
{
Vector3 target = normalLocalPosition;
if (Input.GetMouseButton(1)) target = aimingLocalPosition;
Vector3 desiredPosition = Vector3.Lerp(transform.localPosition, target, Time.deltaTime * aimSmoothing);
transform.localPosition = desiredPosition;
}
void DetermineRecoil()
{
transform.localPosition -= Vector3.forward * 0.1f;
if (randomizeRecoil)
{
float xRecoil = Random.Range(-randomRecoilConstraints.x, randomRecoilConstraints.x);
float yRecoil = Random.Range(-randomRecoilConstraints.y, randomRecoilConstraints.y);
Vector2 recoil = new Vector2(xRecoil, yRecoil);
_currentRotation += recoil;
}
else
{
int currentStep = clipSize + 1 - _currentAmmoInClip;
currentStep = Mathf.Clamp(currentStep, 0, recoilPattern.Length - 1);
_currentRotation += recoilPattern[currentStep];
}
}
IEnumerator ShootGun()
{
_currentAmmoInClip -= 1;
DetermineRecoil();
RayCastEnemy();
yield return new WaitForSeconds(fireRate);
canShoot = true;
}
void RayCastEnemy()
{
RaycastHit hit;
if (Physics.Raycast(transform.parent.position, transform.parent.forward, out hit, 1 << LayerMask.NameToLayer("Enemy")))
{
try
{
Debug.Log("Hit an enemy");
Rigidbody rb = hit.transform.GetComponent<Rigidbody>();
rb.constraints = RigidbodyConstraints.None;
rb.AddForce(transform.parent.transform.forward * 50);
}
catch { }
}
}
IEnumerator FinishShooting()
{
yield return new WaitForSeconds(0.3f);
shootingSound.Stop();
}
IEnumerator Reload()
{
canShoot = false;
yield return new WaitForSeconds(reloadTime);
int amountNeeded = clipSize - _currentAmmoInClip;
if (amountNeeded > _ammoInReserve)
{
_currentAmmoInClip += _ammoInReserve;
_ammoInReserve -= amountNeeded;
canShoot = true;
}
else
{
canShoot = false;
_currentAmmoInClip = clipSize;
_ammoInReserve -= amountNeeded;
canShoot = true;
}
}
}
使用系统集合;
使用UnityEngine;
使用UnityEngine.UI;
使用UnityEngine.Audio;
公共类GunController:MonoBehavior
{
[标题(“枪设置”)]
公共浮式消防车的火灾率=0.1f;
公共int clipSize=30;
公共国际储备容量=270;
//在整个代码中更改的变量
布尔能射;
int _currentAmmoInClip;
国际弹药储备;
//枪口闪光
公众参与制度;
//瞄准
公共向量3正常本地位置;
公共向量3指向本地位置;
公共浮动利率=10;
[标题(“鼠标设置”)]
公共浮动鼠标灵敏度=1;
矢量2_;电流旋转;
公共浮动武器数量=10;
//武器后坐
公共布尔随机数;
公共向量2随机约束;
//仅当“随机化反冲”处于禁用状态时才需要指定
公共向量2[]反冲模式;
//音频
声源射击声;
//重新加载
公共浮子重新加载时间=1.5f;
私有void Start()
{
_CurrentAmmonInclip=剪辑;
_氨储备=储备容量;
Canshot=正确;
shootingSound=GetComponent();
}
私有void更新()
{
DetermineAim();
决定论();
如果(输入。GetMouseButton(0)&&canShoot&&U CurrentAmmonInclip>0)
{
射击声;
开始例行程序(完成拍摄());
口吻闪光。播放();
canShoot=假;
_包括IP--;
开始例行程序(射击枪());
}
else if(Input.GetKeyDown(KeyCode.R)和&&u currentAmmonInclip0)
{
start例程(Reload());
}
}
无效判定法()
{
Vector2 mouseAxis=新的Vector2(Input.GetAxisRaw(“鼠标X”)、Input.GetAxisRaw(“鼠标Y”);
mouseAxis*=鼠标敏感度;
_currentRotation+=鼠标轴;
_currentRotation.y=数学钳位(_currentRotation.y,-90,90);
transform.localPosition+=(Vector3)鼠标轴*武器数量/1000;
transform.root.localRotation=Quaternion.AngleAxis(_currentRotation.x,Vector3.up);
transform.parent.localRotation=Quaternion.AngleAxis(-currentRotation.y,Vector3.right);
}
void DetermineAim()
{
Vector3目标=normalLocalPosition;
如果(Input.GetMouseButton(1))target=aimingLocalPosition;
Vector3 desiredPosition=Vector3.Lerp(transform.localPosition,target,Time.deltaTime*aimsoothing);
transform.localPosition=desiredPosition;
}
void deterecoil()
{
transform.localPosition-=Vector3.forward*0.1f;
if(随机化线圈)
{
float xRecoil=Random.Range(-randomRecoilConstraints.x,randomRecoilConstraints.x);
float yRecoil=Random.Range(-randomRecoilConstraints.y,randomRecoilConstraints.y);
矢量2反冲=新矢量2(X反冲,Y反冲);
_当前旋转+=反冲;
}
其他的
{
int currentStep=clipSize+1-_currentAmmonInclip;
currentStep=Mathf.Clamp(currentStep,0,反冲模式长度-1);
_currentRotation+=反冲模式[currentStep];
}
}
IEnumerator射击枪()
{
_当前IP-=1;
确定线圈();
雷卡斯蒂尼();
产量-返回新的WaitForSeconds(燃速);
Canshot=正确;
}
void raycastenmy()
{
雷卡斯特击中;
if(Physics.Raycast(transform.parent.position、transform.parent.forward、out-hit、1_ammoninreserve)
{
_CurrentAmmonInclip+=\u AmmonInReserve;
_弹药储备-=所需数量;
Canshot=正确;
}
其他的
{
canShoot=假;
_CurrentAmmonInclip=剪辑;
_弹药储备-=所需数量;
Canshot=正确;
}
}
}
我看不到在代码中的任何地方调用函数DetermineRecoil()
,但是这个解决方案可能会起作用:
let _currentRotation = Vector2.Lerp(_currentRotation, _currentRotation + recoilPattern[currentStep], 10*Time.deltaTime);
它将平滑地更改当前旋转的值。您好,您的解决方案不起作用,它使反冲根本不存在,并且确定在IEnumerator射击枪中调用了线圈。