我的子弹随机产生并且没有伤害

问题描述 投票:0回答:1
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;

    }
 }

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

c# unity3d
1个回答
0
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.