[QUESTION]Shooting to mouse location [Solved]

hi there. i we been working on this game for a while after finishing the lecture. i wan to shoot the lasers to the direction of mouse. itried to do something like this in fire method of PlayerController
GameObject beam = Instantiate (laser) as GameObject;
beam.transform.position = transform.position;
var mousePos = Input.mousePosition;
mousePos.z = (transform.position.z - Camera.mainCamera.transform.position.z); //The distance from the camera to the player object
var worldMousePosition = Camera.main.ScreenToWorldPoint (mousePos);
var direction = worldMousePosition - transform.position;
Debug.DrawRay(transform.position, direction.normalized * 2.0f, Color.red, 3.0f);
var angle = Mathf.Atan2 (-direction.x, direction.y) * Mathf.Rad2Deg;
beam.transform.rotation = Quaternion.AngleAxis (angle, Vector3.forward);
beam.rigidbody2D.AddForce (beam.transform.forward * 5000);

everything works great but the only thing is the beam doesnt move. it just rorates perfectly right but doesnt move to that direction. it just stays there.im trying to fix it for days and finally decided to ask you guys can you help me with this please.

1 Like

ok guys no worries i found it. it seems like i ticked kinematic on PlayerLaser prefab… i cant believe i did that -.- i hope it can be helpfull to someone

1 Like

Privacy & Terms