2D Top down projectile help (not moving)
I got a top down rpg, zelda style, a boss and a child on the boss which is the source that throws fireball projectiles. This is the script on the child.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BossAttack2 : MonoBehaviour {
public GameObject fireball;
public GameObject genericfireball;
public GameObject player;
public GameObject fire;
public Rigidbody2D body;
public Transform target;
public Transform location;
public Transform source;
public float speed = 0.5f;
// Use this for initialization
void Start ()
{
player = GameObject.FindWithTag("Player");
fire = GameObject.Find("fire");
source = GetComponent<Transform>();
location = source.transform;
}
// Update is called once per frame
void Update () {
target = player.transform;
location.LookAt(target);
}
public void Fireball()
{
{
genericfireball = Instantiate(fireball, location.position, location.rotation) as GameObject;
genericfireball.GetComponent<Rigidbody>();
body = genericfireball.GetComponent<Rigidbody2D>();
body.velocity = transform.forward * speed;
}
}
}
With this, the fireball comes out of the child object and goes towards player as intended. But the projectile rotates around the y axis, instead of z (which would make the fireball look at the player).
If I understand correctly, with "location.position and location.rotation" it inherits the position of the child and its rotation (that is supposed to look at the player at all times). If I use instantiate without these the instances spawn at a random location and obviously don't turn around z.
Is there any way to make it look at the player with the z axis and leave y untouched?
Update: I used this code in the update function and managed to get the fireball to look at the player. Now the problem is that it's not moving for some reason.
float AngleRad = Mathf.Atan2(target.transform.position.y - source.transform.position.y, target.transform.position.x - source.transform.position.x);
float AngleDeg = (180 / Mathf.PI) * AngleRad;
this.transform.rotation = Quaternion.Euler(0, 0, AngleDeg + 90);
Update: Managed to make it move but it only moves towards a specific direction that is not affected by the player at all... Added body.velocity = new Vector2(target.position.x, target.position.y);
Your answer
![](https://koobas.hobune.stream/wayback/20220612110332im_/https://answers.unity.com/themes/thub/images/avi.jpg)