2

Hi i am currently working on a turret shooting script. My bullet shoots boet does not go in the enemy's direction. something is wrong or missing and i cant figure out what. I have looked up alot of solutions but cant find recent ones or good ones.

EDIT: my enemy's are instantiated so there is multiple of them. would that be part of the problem? if not do i need to provide more details? im kina new to the site so pls forgive me if i do stuff wrong.

public GameObject Enemy;
public GameObject Bullet;
public float bulletForce = 100f;

private Vector3 direction;

// Use this for initialization
void Start ()
{
    ShootFunctionRepeat();  
}

// Update is called once per frame
void Update ()
{
    direction = Enemy.transform.position - this.transform.position;

}

void ShootFunctionRepeat()
{
    InvokeRepeating("ShootFunction", 0.0f, 1.0f);
}

void ShootFunction(GameObject Bullet)
{
    GameObject temp = Instantiate(Bullet, this.transform.position + direction.normalized, Quaternion.identity);
    temp.GetComponent<Rigidbody>().AddForce(direction.normalized * bulletForce);
}
7
  • I think the direction = Enemy.transform.position - this.transform.position; should be in the ShootFunction(GameObject Bullet) method. But this probably doesn't fix the problem. Mar 27, 2017 at 12:34
  • @JeroenvanLangen tried that, that doesnt work. thanks for the suggestion!
    – Jeel drol
    Mar 27, 2017 at 12:39
  • (wild guessing) What about moving the temp.GetComponent<Rigidbody>().AddForce(direction.normalized * bulletForce); to the Update method. (you need to create a field instead of the local variable GameObject temp) Mar 27, 2017 at 12:44
  • @JeroenvanLangen Not sure what you mean by making a field. could you explain?
    – Jeel drol
    Mar 27, 2017 at 12:49
  • I think the AddForce should not be called ones: answers.unity3d.com/questions/567934/… Mar 27, 2017 at 12:57

2 Answers 2

2

Your code won't work. You need to remove the GameObject Bullet paramter from ShootFunction() because the paramter hides this.Bullet. And also Unity3D will print warning message Trying to Invoke method: Shoot.ShootFunction couldn't be called.: you cannot use InvokeRepeating with method parameters.

Now it works:

example

I used a bullet prefab assigned to public GameObject Bullet. And public GameObject Enemy was also assigned a Cube GameObject from the inspector.

Full project.

But you still need to think about how to recycle and finally destroy the bullets: In your code, you just Instantiate the bullets, but when will they be destroyed?

2
  • check out the edit on my post. maybe you will be able to help me now. tried this and it did not work. thanks for helping!
    – Jeel drol
    Mar 27, 2017 at 15:59
  • Added link to full project.
    – zwcloud
    Mar 28, 2017 at 1:31
1

It's not easy to know what exactly you are trying to do. But this will at least fix most issues.

  1. You must always normalize your vectors before they can be used as a direction. A direction is a vector with a magnitude of 1. If you simply subtract one location from another you don't get a normalized vector.
  2. I am not sure if your ShootFunction is allowed to have a parameter. I don't think so. Access you member (Bullet) directly.

  3. I also removed an offset in your instantiation location. You might need to add it back in. But I believe it is not necessary.

Sample

public GameObject Enemy;
public GameObject Bullet;
public float bulletForce = 100f;

private Vector3 direction;

// Use this for initialization
void Start ()
{
    ShootFunctionRepeat();  
}

// Update is called once per frame
void Update ()
{
    direction = (Enemy.transform.position - this.transform.position).normalized;    
}

void ShootFunctionRepeat()
{
    InvokeRepeating("ShootFunction", 0.0f, 1.0f);
}

void ShootFunction()
{
    GameObject temp = Instantiate(Bullet, this.transform.position, Quaternion.identity);
    temp.GetComponent<Rigidbody>().AddForce(direction.normalized * bulletForce);
}
1
  • I'm still getting the same problem. i edited the post where i explained it a bit better.thanks for helping.
    – Jeel drol
    Mar 27, 2017 at 15:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.