unity3d 我的子弹随机产生,没有伤害

lf5gs5x2  于 2023-05-01  发布在  其他
关注(0)|答案(1)|浏览(136)
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项目中为敌人炮塔写的代码。它的想法是,一旦进入射程,它会向玩家发射子弹,当它超出射程时停止,当子弹与玩家碰撞时,玩家会受到伤害,但我很难弄清楚为什么子弹不会造成任何伤害。有什么想法?非常感谢帮助!

bn31dyow

bn31dyow1#

根据我的经验:子弹速度可能太快而无法检测碰撞。
假设在2D中:
帧1:

.BB.......OOO......

帧2:

...........

帧3:

..........OOO ..BB ..

其中OOO是你的对象,BB是你的子弹。
为了解决这个问题,你可以用一个更大的对撞机来处理子弹。其他解决方案,如“动态”对撞机也是可能的。

相关问题