- Home /
Projectile changes direction and angle
I set up a fireball projectile to move based on my player's facing direction, and while it works for the most part there are a couple kinks that I just can't figure out. The main thing is changing direction of the player causes the fireball to change direction. I am hoping maybe I can get assistance to solve this.
The other issue is that the fireball sometimes moves at an angle instead of a straight line, this isn't as huge an issue but it is something I would like to fix as well.
//This code is attached to my player
public void CastSpell()
{
Instantiate(spellPrefab[0], exitPoints[exitIndex].position, Quaternion.identity);
}
this code is attached to my fireball prefab
private void FixedUpdate()
{
GameObject myPlayer = GameObject.Find("Player");
Player aplayer = myPlayer.GetComponent<Player>();
Vector2 moveDirection = gameObject.transform.position - aplayer.transform.position;
myRigidBody.velocity = moveDirection.normalized * speed;
if (moveDirection != Vector2.zero)
{
float angle = Mathf.Atan2(moveDirection.y, moveDirection.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward * speed);
}
}
Answer by N-8-D-e-v · Jun 18, 2020 at 08:03 PM
The fireball changes direction because you have your code in FixedUpdate, which is called (before) every frame, so your fireball will update it's direction
Your answer
Follow this Question
Related Questions
I am having problems with making my character face the movement direction after stopping. 1 Answer
dodge roll in mouse direction in a 2D Top down game,Top down dodge roll in mouse direction 0 Answers
2d top-down rpg direction check 1 Answer
Need help with Raycast2D 0 Answers
Top-down movement in 2D 1 Answer