0

Not sure why, I've done this sort of this a bunch of times, but this is giving me some issues. Making a project for Game AI, I have a whole bunch of stuff already done, now just making some turrets that if the player is in a certain range it will fire, which I have already. The turret fires the bullet and then for some reason they just start destroying themselves and they don't go towards my player. Wondering if anyone on here can help, thanks in advance!

Some details you may need to know: I have a Base, a Gun nose and a Gun for my turret. My Gun has a GunLook.cs script that makes it look at the player (so when they shoot it should go towards them), I'm not sure if that has anything to do with why I'm having these issues, but I'll post that code as well just incase

How my Hierchy for my turret is Turret_AI (Base) Gun (child of Turret_AI) bulletSpawn (child of Gun) Gun_Nose (child of turret_AI)

bulletSpawn is an empty GameObject I created in hopes to solve my problem. I set it just off the Gun so that it wouldn't just collide with gun and destroy itself (what I thought it might be doing, but not correct).

That should be all the info needed, if anyone needs more I will be checking this every 2 seconds so let me know and I will get back to you with quick response.

TurretScript.cs (I did set the GameObject to Player in Unity, before anyone asks)

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);
    }
}
}

BulletBehavior.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);
        }
    }
}
}
ziggystar
  • 28,410
  • 9
  • 72
  • 124
  • Can you explain more clearly what happens to the bullets? Do you mean they start firing then suddenly disappear? Do they move at all? Can you explain in detail - I can't see anything majorly wrong, but I've not really used Unity - I suspect the issue is elsewhere in the codebase though – Charleh Feb 26 '14 at 19:29
  • Can you post the script for the bullets too - it looks like you instantiate them but you don't hold the reference to the copy so I'm assuming you have a bullet script – Charleh Feb 26 '14 at 19:30
  • It's very odd, the bullet does move, I see it instantiate and the first one flies off for like 2 seconds, dies, and then 2 others are frozen at the spawn and I get a NullReferenceException, doesn't tell me why though. I will post Bullet script as well @Charleh –  Feb 26 '14 at 19:57

1 Answers1

1

You're never moving your bullet.

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);
  }
}

Then call it like so:

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");
}

Note: If you're trying to mix this approach with trying to use physics to move the bullet you may end up with strange results.

McAden
  • 13,714
  • 5
  • 37
  • 63
  • I tried this, it did make sense, but then I get another NullReferenceException(unityengine.Transform.get_position() –  Feb 26 '14 at 20:02
  • Just refined my answer. It doesn't make sense to use a coroutine since there's probably more logic to go with it. Plus, if you're destroying the bullet elsewhere it will probably mess with the coroutine. – McAden Feb 26 '14 at 20:06
  • I've never seen this before (or else I would try to figure this out myself, thanks for the help so far) so I thought I would post it: error CS0266: Cannot implicity convert type float? to float. An explicit conversion exists (are you missing a cast?) I saw you used a operator I'm not familiar with (??) I have seen (?) before but never double. So I'm guessing it might have something to do with that? –  Feb 26 '14 at 20:11
  • oops, corrected in the Update function with `.GetValueOrDefault()`. `float?` is shorthand for `Nullable`. `??` is the [Coalesce Operator](http://msdn.microsoft.com/en-us/library/ms173224.aspx). – McAden Feb 26 '14 at 20:14
  • I literally commented out my Bullet script so I don't think I'm using any physics to move it anymore. –  Feb 26 '14 at 20:14