|
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!
(comments are locked)
|
|
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. 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)
|
|
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
(comments are locked)
|
