- Home /
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.
Answer by Peter G · May 30, 2010 at 10:42 PM
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.
Your answer
Follow this Question
Related Questions
Problems with accurate third-person mouse aiming 0 Answers
Mouse Aiming Follow Wrong Direction - Help 2 Answers
Use the mouse to aim? 2 Answers