Question by
N-8-D-e-v · Apr 29, 2020 at 06:38 PM ·
c#scripting problemscriptingproblem
Side-scrolling Enemy AI not shooting in the right direction
I'm making a side scrolling 2D game where the player shoots aliens, and the aliens shoot back. I have it so the camera and the player move and everything else stays the same. When my aliens shoot they always shoot in the same direction not taking into account this script i have
public class LazerShootSide : MonoBehaviour
{
public GameObject player;
public float moveSpeed;
private Rigidbody2D rb;
private Vector2 fireDirection;
public Transform lazerDir;
void Start()
{
rb = GetComponent<Rigidbody2D>();
fireDirection = (player.transform.position - transform.position).normalized * moveSpeed;
rb.velocity = new Vector2(fireDirection.x, fireDirection.y);
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.tag != "alien")
{
Destroy(this.gameObject);
}
}
void OnBecameInvisible()
{
Destroy(this.gameObject);
}
}
Any ideas?
Comment
Your answer
Follow this Question
Related Questions
How to use the GPGS ISavedGameMetadata? 1 Answer
How to rotate RigidBodyFPSController (C#) 1 Answer
Try part of C# not being Read 1 Answer
enemy following issue 0 Answers
How do you copy an image from the clipboard into an ui image? 0 Answers