x


Aiming to the crosshair

Hello,

I'm currently busy on a sort of FPS game, I've got a crosshair in the middle of the screen but I need help with the positioning of the crosshair. I want to shoot to the crosshair, I'm using a raycast to shoot. I could use tips and "tutorial" like answers. I set up a little mini script showing whats the main thing my gun is doing:

var direction = transform.TransformDirection(Vector3.forward);
var hit : RaycastHit;

if(Physics.Raycast(spawn.position, direction, hit, range)){

   }

That's about it what I'm using the rest is just defining what the raycast hit.

Thanks in advance!

-Hybris

more ▼

asked May 05 '12 at 12:57 PM

Hybris gravatar image

Hybris
415 5 8 10

Should be able to find something already posted in here on this. I've seen the same question asked before at least once.

May 05 '12 at 01:09 PM Lo0NuhtiK

I searched but I couldnt find the 1 i was looking for

May 05 '12 at 01:10 PM Hybris
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

You'll probably want to use Camera.ScreenPointToRay(), passing in some useful data like the center of the screen, mouse cursor position, or so on.

You can see an example here: http://answers.unity3d.com/questions/23683/air-strike-shoot-aiming-at-cross-hairs-in-middle-o.html

Here's the script reference page: http://unity3d.com/support/documentation/ScriptReference/Camera.ScreenPointToRay

more ▼

answered May 05 '12 at 06:45 PM

rutter gravatar image

rutter
5.1k 2 11

(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:

x1525
x274
x110
x61

asked: May 05 '12 at 12:57 PM

Seen: 782 times

Last Updated: May 05 '12 at 06:46 PM