x


Firing towards mouse- aim in all directions?

I am using this code to fire a bullet ('launch' prefab) from the player towards the mouse position

(in Update)

 }if(Input.GetKeyDown("b")){
         
         var distFromCam = Camera.main.transform.InverseTransformDirection(transform.position).z ;
         var targetScreenPos = Input.mousePosition;
         targetScreenPos.z = distFromCam;
         var targetPosition = Camera.main.ScreenToWorldPoint(targetScreenPos); 
 
         
             var go: GameObject = Instantiate(launch, fire.position, transform.rotation);
             go.transform.LookAt(targetPosition);


I think this is only paying attention to the camera's actual position instead of its position and rotation- When the camera rotates around 180, it will still only fire on the world axis' z from the camera. How do I tell this to pay attention to the camera's z rotation? Thanks

more ▼

asked Nov 20, 2011 at 05:28 AM

superventure gravatar image

superventure
760 158 134 164

Is this a top-down shooter? An FPS? A TPS? A sidescroller? More info please.

Nov 20, 2011 at 09:22 AM Neten

fps when in firing mode.

Nov 20, 2011 at 07:21 PM superventure
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

This approach will not work, because you can't figure out the distance from the camera to the point you want to shoot. You should use ScreenPointToRay and Physics.Raycast (see example here) to find the world point "under" the mouse pointer:

if(Input.GetKeyDown("b")){
    var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    var hit: RaycastHit;
    if (Physics.Raycast(ray, hit)){ // executes only if some object hit (sky doesn't count)
        var targetPosition = hit.point; 
        var go: GameObject = Instantiate(launch, fire.position, transform.rotation);
        go.transform.LookAt(targetPosition);
        ...
    }
more ▼

answered Nov 20, 2011 at 07:57 PM

aldonaletto gravatar image

aldonaletto
57.9k 34 97 311

Thank you so much for helping me- I see now what was going on, and this does make more sense.

Nov 20, 2011 at 08:08 PM superventure

Whoa, whoa, hold up a sec- There is no exact point I need to shoot at. I think I am not explaining right. The code I am using works, its just that it only works with the world's forward direction (z) instead of the camera's forward rotation. I just need a way to switch that.

Nov 21, 2011 at 01:27 PM superventure

BOOM! FIGURED IT OUT! If you add 360 to end of distFromCam in the original code, you can fire in all directions!

Nov 21, 2011 at 01:36 PM superventure
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3872
x1608
x1472
x198

asked: Nov 20, 2011 at 05:28 AM

Seen: 2473 times

Last Updated: Nov 21, 2011 at 01:36 PM