1
\$\begingroup\$

I am making an FPS, in Unity. I have a player with a gun, and I have a bullet prefab. I changed the mouse pointer to look like a crosshair, but I don't know how to place the bullet where the crosshair is aiming.

The player movement is on the x and z axis. I have done lots of research on the Internet, and couldn't find anything suitable; most of what I found, I could not understand enough to change it according to my needs.

How do I set up a bullet to fire towards the mouse pointer?

\$\endgroup\$
  • \$\begingroup\$ If you're using high velocity bullets it would most likely be best to use Physics.Raycast instead of a bullet prefab. \$\endgroup\$ – Daniel Oct 1 '15 at 17:54
  • \$\begingroup\$ Can you detail it a little more? I'm kinda new to Ray Casting. \$\endgroup\$ – Anonymous Oct 3 '15 at 12:40
  • \$\begingroup\$ It might also help to know what language your using. \$\endgroup\$ – Gnemlock May 5 '17 at 5:33
1
\$\begingroup\$

If you would like to use actual bullet prefab instead of raycast, then what I would do is: (Keeping in mind it's an FPS)

  1. Get the mouse position in world space .ScreenToWorldPoint()

  2. Get direction between camera and that point

  3. Fire a bullet from the camera in that direction (by using instantiate and then adding force) OR you could make the bullet look at that point and add force forwards this could work better as your bullets rotation would be correct

P.S. you may need to adjust an individual axis value for the first step if it's too close to the camera.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.