# Shooting Towards mouse Cursor

 0 I have a sphere in my game that follows the cursor and rotates around my player, but how would I have it so when I press the mouse button (Input.GetMouseButton(0)) is clicked, it fires to the position of the cursor? I'm confused on how to actual move it. I've looked around on here but didn't find anything that worked for me. My bullet prefab is also a rigidbody. Here's how I find the position of the mouse in my 2d game world (X-axis & Y-axis): ``````var plane = Plane(transform.position, Vector3.up); var ray = Camera.main.ScreenPointToRay(Input.mousePosition); var distance: float; if (plane.Raycast(ray, distance)){ hitPoint = ray.GetPoint(distance); hitPoint.z = -19; } ``````This is all done in the function Update(). Thank you for your help. more ▼ asked May 10 '12 at 02:14 PM timo0060 129 ● 12 ● 26 ● 28 You're mostly there- once you have the 'HitPoint', you can get the direction that the object needs to be fired in using ``````var direction : Vector3 = hitPoint - transform.position; ``````Then, just use whatever system you may want to use for your bullets, and that vector. May 10 '12 at 02:25 PM syclamoth I can't quite wrap my mind around how to do that. I just don't fully understand how to implement the vector with the transform.translate of my bullet. May 10 '12 at 05:04 PM timo0060 You may want to look more at rigidbody. You could do something like setting the velocity based on the desired vector, or apply a force in that direction. May 10 '12 at 05:48 PM Julien.Lynge Thank you for your advice. I'll certainly look into it. Thank you both. May 10 '12 at 06:28 PM timo0060 Use 'Transform.translate(direction * speed * Time.deltaTime);'it's not that hard. May 11 '12 at 03:26 AM syclamoth add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

1 answer: sort voted first
 0 I had a similar problem, although I was not looking for the object to go to where the mouse is. This is what I came up with however. `````` var bullet : Rigidbody; var speed : float = 10.0f; var muzzlePoint : Transform; function Update() { if(Input.GetButtonDown("Fire1")) { bullet.useGravity = false; var instance : Rigidbody = Instantiate(bullet, muzzlePoint.position, muzzlePoint.rotation); instance.velocity = muzzlePoint.forward * 50; } } ``````I hope this helps. more ▼ answered May 11 '12 at 01:07 AM tw1st3d 105 ● 3 ● 4 This code doesn't exactly go to the mouse though does it? May 11 '12 at 01:46 AM timo0060 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users
Your answer

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

x1070
x1001
x285

asked: May 10 '12 at 02:14 PM

Seen: 1403 times

Last Updated: May 11 '12 at 10:59 AM