- Home /
Fire at last known Player Location 2D
Hi there,
So I've been trying to get my 2D dragon to fire at player's location.. and for some odd reason when it does fire...it shoots it on a 3D plain instead of the 2D and towards the player and they just shoot straight...
So all and all, it shoots straight on the Z plane and the sprite is not "flat"
Reference Image 
I wanna assume its the Quaternion but I could be wrong...
Any help would be appreciated!
public class DragonFireBall : MonoBehaviour {
public Transform Player;
public float speed = 3;
// Use this for initialization
void Start()
{
Vector2 playerPos = new Vector2(Player.position.x, Player.position.y + 1);
transform.rotation = Quaternion.LookRotation(playerPos);
}
// Update is called once per frame
void Update()
{
transform.position += transform.forward * speed * Time.deltaTime;
}
void OnTriggerStay2D(Collider2D collision)
{
if (collision.gameObject.tag == "Player")
{
Destroy(gameObject);
// GetComponent<AttacksandPowers>().Health;
}
}
}
Dragon Script in case its needed
public Transform Player;
public float speed;
public GameObject Fireball;
public Vector2 lastPos;
public bool FiredFire = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (FiredFire == true)
{
// StartCoroutine(CanFire());
}
// lastPos = new Vector2(Player.transform.position.x, Player.transform.position.y + 1);
}
void OnTriggerStay2D(Collider2D collision)
{
if (collision.gameObject.tag == "Player")
{
transform.position = Vector2.MoveTowards(transform.position, Player.position, speed * Time.deltaTime);
if (FiredFire == false)
StartCoroutine(FireBall());
}
}
IEnumerator FireBall()
{
yield return new WaitForSeconds(5f);
Instantiate(Fireball, transform.position, transform.rotation);
}
}
Answer by KittenSnipes · Jan 29, 2018 at 10:33 AM
Most likely it is this line of code on your dragon:
Instantiate(Fireball, transform.position, transform.rotation);
Try changing it to this:
Instantiate(Fireball, transform.position, new Quaternion(transform.rotation.x + 90, transform.rotation.y, transform.rotation.z + 90, transform.rotation.w));
I recommend playing around with the values a bit. so if 90 doesnt work then do 180 then do 270. Just use values of 90 because on it is on its side. Do not change transform.rotation.w Cheers mate
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Sound effect issue 1 Answer
Object Reference not set to instance of an object - error in 2d rope code 0 Answers
What can I do to further optimize mobile game? (2D) 0 Answers