【问题标题】:My bullet spawns randomly and does no damage我的子弹随机产生并且没有伤害
【发布时间】:2019-05-13 01:46:40
【问题描述】:
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++ 而不是 Java?这一定是我见过的最奇怪的 C++ 代码。
  • 它既不是 c++ 也不是 Java。那是 C#。
  • 不知道为什么,这是炮塔的代码,给我们看看玩家应该受到伤害的代码。
  • void OnTriggerBullet(Collision coll) { if(coll.gameObject.tag.Equals ("Bullet")) { Destroy(gameObject); } UnityEngine.SceneManagement.SceneManager.LoadScene(levelToLoad); } 这是我的玩家脚本中我的玩家遇到子弹的地方。这个想法是,如果玩家被子弹击中,玩家就会被摧毁并触发游戏结束屏幕。

标签: c# unity3d bullet


【解决方案1】:

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

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

【讨论】:

  • 还有光线投射技术,您可以检查物体前面的空间以及它移动的距离以检查障碍物
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-13
  • 2016-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-22
相关资源
最近更新 更多