The question is answered, right answer was accepted
Enemy projectile direction setting
Hey everyone. I've run into an issue with using "Turret-type" enemies in a simple 2D game, programmed to fire a projectile based on the direction and rotation it's currently in. The problem is that when I have two facing different directions, their projectiles get 'confused' as to which way to go, is the best way to put it. I have tested it with just one enemy and it works fine. Is there a better way to make sure that the bullet 'knows' specifically which direction it was fired from? Thanks in advance. For reference, here's the code for the bullet in question I'm using:
public class EBullet1Behavior : MonoBehaviour {
private Rigidbody2D bulletRigi;
public Playermovement playerMovement;
public EnemyType2Properties enemyType2;
public FindVector findVector;
public Transform bulletPoint;
public float speed;
public float destroyTime;
// Use this for initialization
void Start () {
playerMovement = FindObjectOfType<Playermovement> ();
enemyType2 = FindObjectOfType<EnemyType2Properties> ();
findVector = FindObjectOfType<FindVector>();
bulletRigi = GetComponentInParent<Rigidbody2D> ();
bulletRigi.velocity = new Vector2 (findVector.x * speed, findVector.y * speed);
transform.right = new Vector3(findVector.x, findVector.y, 0);
}
// Update is called once per frame
void Update () {
Destroy (gameObject, destroyTime);
}
}
While the firing bit is fairly simple:
IEnumerator Firing() {
if (stunned == false && canFire == true)
{
Instantiate(EnemyBullet, bulletPoint.position, bulletPoint.rotation);
yield return new WaitForSeconds(fireSpeed);
Debug.Log("Fire!");
canFire = true;
// Firing capability is turned 'off' elsewhere in the script
Update();
}
}
GameObject bullet = Instantiate (....);
bullet.GetComponent<EBullet1Behavior>().findVector = something;
like that...
Answer by Baste · Jan 27, 2016 at 04:43 PM
The bullet has a ton of properties it doesn't need to have. It's also getting it's direction by finding a FindVector object, and grabbing information from that.
The FindObjectOfType method finds an object of that type. Which object it finds is not guaranteed, but it's usually the same one for every call on the same frame, so both your guns' bullets are finding the same FindVector object.
The solution here is to not use the convoluted FindVector method, and skip the start method. Instead, give the bullet a Fire method that takes the direction. So along the lines of:
//bulletscript
public class BulletScript : MonoBehaviour {
public float speed; //set in prefab
public float destroyAfter; // set in prefab
public void Fire() {
GetComponent<RigidBody>().velocity = transform.forward * force;
Destroy(gameObject, destroyAfter);
}
}
That's really all you need. Your firing code will simply be:
GameObject bulletObj = Instantiate(EnemyBullet, bulletPoint.position, bulletPoint.rotation);
bulletObj.Fire();
The Instantiate call sets the rotation of the bullet. This means that if the Fire-code sets the velocity straight forward (transform.forward), it should go straight ahead.
Hey again. I should probably have specified that I'm working with Unity2D, so I made the simple adjustment of setting the transform.forward to transform.right. Unfortunately, now all my bullets are moving rightwards regardless of which way the enemy is facing. I'll try and work on this myself, but any pointers would be appreciated.
Follow this Question
Related Questions
2D Weapon Rotation Behaviour Changes on Flipping Sprite? 1 Answer
How do I get the enemy's to rotate to players and shoot at them? 0 Answers
Bullet goes sideways when the player gets hit 1 Answer
How to make my snowball model to actually shoot like a projectile? 0 Answers
2.5D Projectile Help 1 Answer