2d shooting,Shooting bullets 2d
I have watched a youtube tutorial about shooting bullets from my character on direction with my mouse position,and the main code is this:
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3 difference = mousePosition - transform.position;
if (Input.GetMouseButtonDown(0))
{
float distance = difference.magnitude;
Vector2 direction = difference / distance;
direction.Normalize();
fireBullet(direction);
}
Please tell me if i am wrong since i am still learning.The vector3 difference will store the sides of the right triangle which is formed by mouse position and my gun.Then the distance is the length of the hypotenuse (basically the distance between them).I don't understand what line 2 and 3 in the if statement do.Why is direction = difference / distance?I think the difference itself is the direction.I know normalize makes the vector's length 1 too.But why?I tried removing those 2 lines , and it would still shoot , but if the closer my mouse is to the gun , the slower it will shoot.Thanks for the help!
Your answer
Follow this Question
Related Questions
2D Platfformer 0 Answers
Handle order in layer in 2D game ? 2 Answers
2D Android Game Rapid Jumping Problem,Unity 2018.2.9f1 Android 2d Speed Jumping 0 Answers
A 3D scene in Unity 2D 0 Answers
2D Gun following rotation problem 0 Answers