- Home /
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!
Answer by Molix · Apr 23, 2010 at 06:35 PM
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);
}
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?
$$anonymous$$ake 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);
Unfortunately that doesn't seem to work for me. $$anonymous$$y 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;
Never$$anonymous$$d, I got it! Thanks again for your help :)
Answer by Ashkan_gc · Apr 23, 2010 at 06:59 PM
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
Your answer
Follow this Question
Related Questions
Third person camera aim 0 Answers
my gun wont follow my mouse 0 Answers
Flamethrower!!1 How to aim emitter at cursor 0 Answers
Reticle orbits around player depending on right stick direction? 0 Answers
2.5d game gun aim 1 Answer