- Home /
How can i fix this shooting code in a 2D plane?
At the moment, this code fires towards the camera for some, unknown reason. What is the fix and explanation of why this is happening?
GameObject firedSpell = Instantiate(spell[currentSpell], new
Vector3(transform.position.x, transform.position.y, transform.position.z),
Quaternion.identity) as GameObject;
firedSpell.GetComponent<Rigidbody2D>().gravityScale = 0.0f;
Vector2 target = Camera.main.ScreenToWorldPoint(new Vector2(Input.mousePosition.x, Input.mousePosition.y));
Vector2 myPos = new Vector2(transform.position.x, transform.position.y + 1);
Vector2 direction = target - myPos;
direction.Normalize();
firedSpell.GetComponent<Rigidbody2D>().velocity = direction * 15;
Comment