0
votes

I am making a 2D platformer with Unity and I have run into a small problem. I have enemies that shoot bullets at the player. However, the bullets are only shooting in one direction regardless of which direction the enemy is facing.

In my Enemy Script I have this:

Instantiate(bullet, spawnPosition.position, Quaternion.identity);

and in my bullet script I have this

rigidbody2D.velocity = new Vector2(bulletSpeed,0);

Please help if you can. I understand why this is happening, but I can not figure out a solution. To update my question I want to be able to check the enemies direction so that I can change the bullet speed to positive/negative to match the direction. Since there will be multiple enemies of this type I do not know how to do this.

public class bulletScript : MonoBehaviour {

    // Use this for initialization
    private float bulletSpeed;
    GameObject parent;

    private Vector3 theScale;

    void Start () {

        rigidbody2D.velocity = new Vector2(bulletSpeed,0);
    }

    // Update is called once per frame
    void Update () {

    //  if(transform.localScale.x < 0) bulletSpeed = -100;
    //  if(transform.localScale.x > 0) bulletSpeed = 100;
    }
    public void SetEnemy(GameObject obj)
    {
        parent = obj;
    }

Then in HammerScript.cs

    public class HammerScript : MonoBehaviour {


    public bulletScript bullet;
    public Transform spawnPosition;


    void FixedUpdate () 
    {

        instantiate(bullet, spawnPosition.position, Quaternion.identity);
        ((bulletScript)bullet).SetEnemy(this);                  
    }




    }

2 new errors: 1-Assets/Scripts/Level 2/HammerScript.cs(89,64): error CS1502: The best overloaded method match for bulletScript.SetEnemy(UnityEngine.GameObject)' has some invalid arguments 2-Assets/Scripts/Level 2/HammerScript.cs(89,64): error CS1503: Argument#1' cannot convert HammerScript' expression to typeUnityEngine.GameObject'

2
Please show the entire relevant sections of code (including the class declaration).Jashaszun
I am now getting this error : Assets/Scripts/Level 2/HammerScript.cs(88,48): error CS1061: Type UnityEngine.GameObject' does not contain a definition for SetEnemy' and no extension method SetEnemy' of type UnityEngine.GameObject' could be found (are you missing a using directive or an assembly reference?)user1569940
You should change Enemy parent; to GameObject parent;.Jashaszun
Please check my answer again. You need to cast bullet to a bulletScript.Jashaszun
What is the new error?Jashaszun

2 Answers

2
votes

the bullets are only shooting in one direction regardless of which direction the enemy is facing.

Well, unless bulletSpeed is positive or negative depending on the enemy's direction, every bullet will have the same speed and direction. In your code, the bullets' velocities do not depend on the enemies' directions at all.

What you could do is keep a reference in the bullet to the enemy that it came from, and then set the velocity according to that enemy's info.

You can do that by having a SetEnemy method that takes an Enemy as a parameter in your Bullet class, and then you can just call ((bulletScript)bullet).SetEnemy(this); immediately after the Instantiate call.

So your HammerScript file should look like this:

public class HammerScript : MonoBehaviour
{
   public bulletScript bullet;
   public Transform spawnPosition;

   void FixedUpdate () 
   {
       instantiate(bullet, spawnPosition.position, Quaternion.identity);
       ((bulletScript)bullet).SetEnemy(this);                  
   }
}

Then, in your Bullet class you can have the following:

class bulletScript : MonoBehavior
{
    GameObject parent;
    public void SetEnemy(GameObject obj)
    {
        parent = e;
    }

    // ... whatever else you have, including the method that sets the velocity
}

Then in your bullet script you would set rigidbody2D.velocity to something that has to do with the enemy (which would be this.parent).

0
votes

Your current implementation only allow the bullet to go into one direction. Let's assume your bulletSpeed is always positive.

If you consider another direction with the angle alpha from that base direction, then you need to create a rotated Vector, which you can do like this:

new Vector2(bulletSpeed * Mathf.Cos(alpha), bulletSpeed * Mathf.Sin(alpha));

Also consider that alpha has to be given as radians, if you truly want to use degrees calculate the radians first:

alpha = (degrees / 180) * 90