我想让我的网络在我的2D game.For中有一个射击场,如果子弹超出射程,那么销毁bullet.Is如果我不使用RayCast会有问题吗?我的Wepon脚本:
//Initialization
public GameObject Bullet;
public Transform FirePoint;
//WeponStats
public float BulletSpeed;
public int Damage;
public float Range;
private float TimeBtwShots;
public float StartTimeBtwShots;
private void Start()
{
}
private void Update()
{
if (TimeBtwShots <= 0)
{
if (Input.GetButton("Fire1"))
{
Shoot();
TimeBtwShots = StartTimeBtwShots;
}
}
else
{
TimeBtwShots -= Time.deltaTime;
}
}
void Shoot()
{
GameObject bullet2 = Instantiate(Bullet, FirePoint.position, FirePoint.rotation);
Rigidbody2D rb = bullet2.GetComponent<Rigidbody2D>();
rb.AddForce(FirePoint.right * BulletSpeed, ForceMode2D.Impulse);
}至于子弹:
public float Delay;
void Start()
{
Destroy(gameObject, Delay);
}
public void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.CompareTag("Enemy"))
{ //take damage function, nothing important
Destroy(gameObject);
Wepon_Script script_W = GameObject.FindGameObjectWithTag("Wepon").GetComponent<Wepon_Script>(); //Wepon script
Enemy_Core script_E = GameObject.FindGameObjectWithTag("Enemy").GetComponent<Enemy_Core>(); //Enemy script
script_E.TakeDamage(script_W.Damage);
}
}发布于 2020-06-17 20:04:27
看起来你已经在延迟一段时间后销毁子弹了:
Destroy(gameObject, Delay);如果你想让子弹在经过一段距离后被销毁,你可以将距离除以速度。
Destroy(gameObject, Range / BulletSpeed);如果你想测量动态行进的距离,你可以在子弹里跟踪它。
void FixedUpdate()
{
var distanceDelta = (transform.position - lastPos).magnitude;
distanceTravelled += distanceDelta;
lastPos = transform.position;
if (distanceTravelled > Range) Destroy(gameObject);
}https://stackoverflow.com/questions/62427323
复制相似问题