0

不知道为什么,我已经做了很多次这样的事情,但这给了我一些问题。为 Game AI 制作一个项目,我已经完成了一大堆东西,现在只制作一些炮塔,如果玩家在一定范围内就会开火,我已经这样做了。炮塔发射了子弹,然后由于某种原因它们开始自毁,并没有朝我的玩家前进。想知道这里有没有人可以帮忙,提前谢谢!

您可能需要了解的一些细节:我的炮塔有一个底座、一个枪头和一个枪。我的枪有一个 GunLook.cs 脚本,让它看着玩家(所以当他们射击时它应该朝向他们),我不确定这是否与我遇到这些问题的原因有关,但我我也会发布该代码以防万一

我的炮塔的层次结构是 Turret_AI (Base) Gun (Turret_AI 的子代) bulletSpawn (Gun 的子代) Gun_Nose (turret_AI 的子代)

bulletSpawn 是我创建的一个空游戏对象,希望能解决我的问题。我把它放在枪上,这样它就不会与枪相撞并摧毁自己(我认为它可能在做什么,但不正确)。

这应该是所需的所有信息,如果有人需要更多信息,我将每 2 秒检查一次,所以请告诉我,我会尽快回复您。

TurretScript.cs(在有人问之前,我确实在 Unity 中将 GameObject 设置为 Player)

using UnityEngine;
using System.Collections;

public class TurretScript : MonoBehaviour {
[SerializeField]
public GameObject Bullet;
public float distance = 3.0f;
public float secondsBetweenShots = 0.75f;
public GameObject followThis;
private Transform target;
private float timeStamp = 0.0f;

void Start () {
    target = followThis.transform;
}

void Fire() {
    Instantiate(Bullet, transform.position , transform.rotation);
    Debug.Log ("FIRE");
}

void Update () {

    if (Time.time >= timeStamp && (target.position - target.position).magnitude < distance) {
        Fire();
        timeStamp = Time.time + secondsBetweenShots;
    }
}
}

GunLook.cs

// C#
using System;
using UnityEngine;

public class GunLook : MonoBehaviour
{
public Transform target;

void Update()
{
    if(target != null)
    {
        transform.LookAt(target);
    }
}
}

子弹行为.cs

using UnityEngine;
using System.Collections;

public class BulletBehavior : MonoBehaviour {

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
    if (rigidbody.velocity.magnitude <= 0.5)
                    Destroy (gameObject);
}

void OnCollisionEnter(Collision collision)
{
    if (collision.collider)
    {
        if(collision.gameObject.tag == "Enemy" || collision.gameObject.tag == "EnemyProjectile")
        {
               Physics.IgnoreCollision(rigidbody.collider,collision.collider);
            //Debug.Log ("Enemy");
        }
        if(collision.gameObject.tag == "SolidObject")
        {
            Destroy(gameObject);
        }
        if(collision.gameObject.tag == "Player")
        {
            Destroy(gameObject);
        }
    }
}
}
4

1 回答 1

1

你永远不会移动你的子弹。

public class BulletBehavior : MonoBehaviour
{
  private const float DefaultSpeed = 1.0f;
  private float startTime;

  public Vector3 destination;
  public Vector3 origin;
  public float? speed;

  public void Start()
  {
    speed = speed ?? DefaultSpeed;
    startTime = Time.time;
  }

  public void Update()
  {
    float fracJourney = (Time.time - startTime) * speed.GetValueOrDefault();
    this.transform.position = Vector3.Lerp (origin, destination, fracJourney);
  }
}

然后像这样调用它:

void Fire() 
{
    GameObject bullet = (GameObject)Instantiate(Bullet, transform.position , transform.rotation);
    BulletBehavior behavior = bullet.GetComponent<BulletBehavior>();
    behavior.origin = this.transform.position;
    behavior.destination = target.transform.position;
    Debug.Log ("FIRE");
}

注意:如果您尝试将这种方法与尝试使用物理来移动子弹相结合,您最终可能会得到奇怪的结果。

于 2014-02-26T19:40:08.917 回答