using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Shooting : MonoBehaviour
{
[SerializeField]
private Transform[] firePoints;
[SerializeField]
private Rigidbody projectilePrefab;
[SerializeField]
private float launchForce = 700f;
[SerializeField]
private Animator anim;
[SerializeField]
private bool automaticFire = false;
[SerializeField]
private bool slowDownEffect = false;
private bool coroutineEnded = false;
private void Start()
{
anim.SetBool("Shooting", true);
}
public void Update()
{
if (isAnimationStatePlaying(anim, 0, "AIMING") == true)
{
if (Input.GetButtonDown("Fire1") && automaticFire == false)
{
if (anim.GetBool("Shooting") == true)
{
anim.Play("SHOOTING");
LaunchProjectile();
}
}
else if (Input.GetButtonDown("Fire1") && automaticFire == true)
{
automaticFire = false;
}
else
{
if (Input.GetButtonDown("Fire2"))
{
automaticFire = true;
}
if (automaticFire == true)
{
anim.Play("SHOOTING");
LaunchProjectile();
}
}
}
}
private void LaunchProjectile()
{
foreach (var firePoint in firePoints)
{
Rigidbody projectileInstance = Instantiate(
projectilePrefab,
firePoint.position,
firePoint.rotation);
projectileInstance.AddForce(new Vector3(0, 0, 1) * launchForce);
StartCoroutine(AddDrag(5, 5, projectileInstance));
if (coroutineEnded == true)
{
projectileInstance.gameObject.AddComponent<BulletDestruction>().Init();
}
}
}
IEnumerator AddDrag(float maxDrag, float mul, Rigidbody rb)
{
float current_drag = 0;
while (current_drag < maxDrag)
{
current_drag += Time.deltaTime * mul;
rb.drag = current_drag;
yield return null;
}
rb.velocity = Vector3.zero;
rb.angularVelocity = Vector3.zero;
rb.drag = 0;
coroutineEnded = true;
}
bool isAnimationStatePlaying(Animator anim, int animLayer, string stateName)
{
if (anim.GetCurrentAnimatorStateInfo(animLayer).IsName(stateName))
return true;
else
return false;
}
}
我尝试使用StartCoroutine添加Drag。问题在于,它正在拍摄第一个气泡和第一个子弹类型的减速并最终停止,然后在尝试发射下一个子弹时出现异常:
MissingReferenceException:“刚体”类型的对象已被破坏,但您仍在尝试访问它。
在线:
rb.drag = current_drag;
我不确定添加拖动是否正确,应该输入什么值。我尝试了5和5。
想法是像减速效果一样只减速子弹。
我为减速做了一个脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SlowDown : MonoBehaviour
{
[Header("Overall Slowdown")]
[Range(0,1f)]
public float overallSlowdown = 1f;
[Space(5)]
[Header("Bullet Time")]
[Range(0, 50)]
public float bulletTime = 0.25f;
private void Update()
{
Time.timeScale = overallSlowdown;
}
}
但是timeScale减慢了整个游戏的速度,我也希望能够仅减慢子弹的速度。
答案 0 :(得分:1)
private void LaunchProjectile()
{
foreach (var firePoint in firePoints)
{
Rigidbody projectileInstance = Instantiate(
projectilePrefab,
firePoint.position,
firePoint.rotation);
projectileInstance.AddForce(new Vector3(0, 0, 1) * launchForce);
StartCoroutine(AddDrag(5, 5, projectileInstance));
}
}
IEnumerator AddDrag(float maxDrag, float mul, Rigidbody rb)
{
float current_drag = 0;
while (current_drag < maxDrag)
{
current_drag += Time.deltaTime * mul;
rb.drag = current_drag;
yield return null;
}
rb.velocity = Vector3.zero;
rb.angularVelocity = Vector3.zero;
rb.drag = 0;
rb.gameObject.AddComponent<BulletDestruction>().Init();
}
调用StartCoroutine()
不会停止其所在方法的执行,因此该协程方法之外的“等待协程完成”是没有意义的(因为该协程可能有多个副本!)
等待协程完成的唯一合乎逻辑的地方是在另一个协程内部或您正在等待的方法内部。当您要修改的对象传递到协程时,将所需的逻辑插入协程方法是一种逻辑选择。