make an enemy spell move towards player
Hi, I'm working on a 2D boss level with a simple set : my player on the left side of the screen and my boss on the right. The player is moving, the boss isn't. The boss is a lot taller than the player and shoots fireballs from its mouth (which is as for now just a gameobject with a particle system attached on it for testing purposes). Problem is they are going straight so they never hit my player.
I'm instantiating the attack with a script attached to my boss and setting the damage values with another script attached to my spell prefab. I tried to add a target gameobject and translate the position towards it but it just spawns right on my player.
Is there any way to add some lines on my script to get it working? It's my first game project so the code might be messy.
Thanks for your help!
public class MonsterAttack : MonoBehaviour
{
public GameObject spell;
public GameObject spellSpawn;
public GameObject spellSpawn2;
public float spellForce;
private Animator anim;
private void Start()
{
anim = GetComponentInChildren<Animator>();
}
void Update ()
{
if(Input.GetButtonDown("Fire2") && anim.GetBool("isSitting") == false)
{
GameObject newSpell = Instantiate(spell, spellSpawn.transform.position, spellSpawn.transform.rotation);
newSpell.GetComponent<Rigidbody2D>().AddRelativeForce(new Vector2(-spellForce, 0f));
anim.SetBool("isYelling", true);
}
else if(!Input.GetButtonDown("Fire2") && anim.GetBool("isSitting") == false)
{
anim.SetBool("isYelling", false);
}
if(Input.GetButtonDown("Fire2") && anim.GetBool("isSitting") == true)
{
GameObject newSpell = Instantiate(spell, spellSpawn2.transform.position, spellSpawn2.transform.rotation);
newSpell.GetComponent<Rigidbody2D>().AddRelativeForce(new Vector2(-spellForce, 0f));
anim.SetBool("isYellingSit", true);
}
else if (!Input.GetButtonDown("Fire2") && anim.GetBool("isSitting") == true)
{
anim.SetBool("isYellingSit", false);
}
}
}
Answer by tormentoarmagedoom · Apr 17, 2018 at 08:31 AM
Good day.
You can calculate the direction vector boss-player and then add force in that direction.
Thank you! Took me a while to figure that one out but you pointed me in the right direction :)