x


Aiming With my mouse

Hi, it's me again. I have a little space ship which stay's in the middle of my screen (Good) I have followed a tut by the Tornado Twins to make a object fire from a spawnpoint in a single directcion or towards were my character is faceing (Good, but not what I need) I would like a script to make a object fir from where my mouse is aiming. thank you for your time.

more ▼

asked May 30 '10 at 08:16 PM

Quentin gravatar image

Quentin
30 4 4 6

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

See the FPS tutorial for weapon aiming. FPS Tutorial

Essentially, you just cast a ray with Camera.ScreenPointToRay(); Here's a simple example.

// Draws a line in the scene view going through the mouse position.
// from the lower-left corner of the screen

function Update () {

    var hit : RaycastHit; //unnecessary in this example.

    var ray = Camera.main.ScreenPointToRay (Input.mousePosition); //ray from
    // through the mousePosition.

    if(Physics.Raycast(ray, hit, Mathf.Infinity)) { //does the ray collide with 
    // anything.

         //add stuff here for finding type of object and such.
         Debug.Log("Hit Something at mouse position");

    }

    Debug.DrawRay (ray.origin, ray.direction * 10, Color.yellow);
    //Display the ray.
}

//untested watch out for typos.
more ▼

answered May 30 '10 at 10:42 PM

Peter G gravatar image

Peter G
15k 16 44 136

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

x983
x103
x61
x44

asked: May 30 '10 at 08:16 PM

Seen: 1813 times

Last Updated: Nov 06 '10 at 05:40 AM