gpt4 book ai didi

c# - 我的子弹随机产生并且不会造成伤害

转载 作者:行者123 更新时间:2023-11-30 12:54:43 25 4
gpt4 key购买 nike

using UnityEngine;    
using System.Collections;
using System;

public class TurretScript : MonoBehaviour
{

public float rangeToPlayer;
public GameObject bullet;
// public GameObject spherePrefab;
private GameObject player; // Tag for DynamicWaypointSeek
private bool firing = false; //Firing status
private float fireTime; // Fire Time
private float coolDown = 1.00F; // Time to cool down fire
private int health = 5; //Health of turret
private bool bDead;
private Action cur;

void Start()
{
player = GameObject.FindWithTag("Player");
bDead = false;
}



void Update()
{
if (PlayerInRange())
{ // PlayerInRange is bool function defined at the end
transform.LookAt(player.transform.position); //Rotates the transform (weapon) so the forward vector points at /target (Player)/'s current position
RaycastHit hit;
if (Physics.SphereCast(transform.position, 0.5F, transform.TransformDirection(Vector3.forward), out hit))
{ //The center of the sphere at the start of the sweep,The radius of the sphere,The direction into which to sweep the sphere.
if (hit.transform.gameObject.tag == "Player")
{ //information in hit (only interested in "Player")
if (firing == false)
{
firing = true;
fireTime = Time.time; //keep the current time
GameObject bul;
Quaternion leadRot = Quaternion.LookRotation(player.transform.position); //Calculate the angular direction of "Player";
bul = Instantiate(bullet, transform.position, leadRot) as GameObject; // existing object to be copied, Position of Copy, Orientation of Copy
//Destroy(bullet, 2.0f);
}
}
}
}
if (firing && fireTime + coolDown <= Time.time) // prvious time stored in fireTime + cool down time is less --> means the firing must be turn off
firing = false;
// Destroy(GameObject.FindGameObjectWithTag("Bullet"));

if (health <= 0)
cur = Deadstate;


}

protected void Deadstate()
{
if (!bDead)
{
bDead = true;
Explode();
}
}

void Explode()
{
Destroy(gameObject, 1.5f);

}


bool PlayerInRange()
{
return (Vector3.Distance(player.transform.position, transform.position) <= rangeToPlayer); //Vector3.Distance(a,b) is the same as (a-b).magnitude.
}

void OnTriggerEnter(Collider col)
{
HealthBarScript.health -= 10f;

}

}`

这是我在当前的 unity 项目中为敌方炮塔编写的代码。这个想法是,一旦它在范围内,它就会向玩家发射一颗子弹,并在它超出范围时停止,当子弹与玩家碰撞时,玩家会受到伤害,但我正在努力弄清楚为什么子弹赢了'做任何损害。想法?非常感谢您的帮助!

最佳答案

根据我的经验:子弹速度可能太快,无法检测到碰撞。
想象一下,在 2d 中:
第 1 帧:
.BB.......OOO......
第 2 帧:
.......BBOOO......
第 3 帧:
........噢..BB..
OOO 是你的对象,BB 是你的子弹。

要解决此问题,您可以使用更大的子弹对撞机。其他解决方法,如“动态”对撞机也是可能的。

关于c# - 我的子弹随机产生并且不会造成伤害,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53734822/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com