x


Aiming in the direction of cursor in third person.

I have a third person game where the cursor will seldom be settled in the middle of the screen, however the character is always centered in the middle of the screen (more or less). I need to be able to shoot projectiles in the direction of the cursor from my character model. I think I may have to use a dummy object as an "aim target" that follows the mouse, but I'm lost on how to implement this. Thanks in advance!

more ▼

asked Apr 23 '10 at 05:27 PM

Nathaniel gravatar image

Nathaniel
5 2 2 3

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

2 answers: sort voted first

You can use a Raycast through the mouse position to find out what 3D geometry the mouse pointer is over, and then create your projectile at your player's location, heading toward that point. e.g.

var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 100)) 
{
  // Get your player object's position (could be any way, this is just one possible way)
  var playerT = GameObject.FindWithTag("Player").transform;
  // Now you have where your player is and a 3D point the mouse is over
  Debug.DrawLine (playerT.position, hit.point);
}
more ▼

answered Apr 23 '10 at 06:35 PM

Molix gravatar image

Molix
4.8k 16 27 66

This worked nicely, thank you!

Apr 23 '10 at 11:29 PM Nathaniel

I have one quick question regarding this actually, how would I go about instantiating a projectile to travel along the line that we've created?

Apr 23 '10 at 11:42 PM Nathaniel

Make a projectile prefab with a rigidbody in it. Then something like:

GameObject projectile = Instantiate(projectilePrefab, playerT.position, Quaternion.identity) as GameObject; projectile.transform.LookAt(hit.point); projectile.rigidbody.velocity = projectile.transform.forward * projectileSpeed; Destroy(projectile, 2);

Apr 24 '10 at 03:30 AM Molix

Unfortunately that doesn't seem to work for me. My projectiles seem to jet off in undesirable angles still. Any other thoughts? Sorry to be such a trouble! Here is the exact code I'm using: var playerT = GameObject.FindWithTag("FirePoint").transform; Debug.DrawLine (playerT.position, hit.point); var clone = Instantiate(projectile, playerT.position, Quaternion.identity) as GameObject; clone.transform.LookAt(hit.point); clone.rigidbody.velocity = projectile.transform.forward * 100;

Apr 24 '10 at 03:49 AM Nathaniel

Nevermind, I got it! Thanks again for your help :)

Apr 24 '10 at 10:30 PM Nathaniel
(comments are locked)
10|3000 characters needed characters left

for more info: you can get the direction of mouse pos and middle of the screen vector using Vector2 class methods. also if you want to know what point in 3d space mouse is on, then you can use camera.ScreenToWorldPoint as z argument you should get the distance from the camera that you want x,y to be calculated. then you can use that point to look at it and place a target object in the position of the mouse if needed. i think in your game you just need to get the direction between mouse position and center of the screen and then use that direction for (y axis rotation of your player) see the evac city tutorial for an example

more ▼

answered Apr 23 '10 at 06:59 PM

Ashkan_gc gravatar image

Ashkan_gc
9.1k 33 56 117

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

x253
x198
x103
x90

asked: Apr 23 '10 at 05:27 PM

Seen: 3626 times

Last Updated: Apr 23 '10 at 05:27 PM