- Home /
Side Scrolling Shooter. How to aim my bullet trajectory at the mouse cursor?
I know this question may sound familiar but I cannot find any answers for this nor can, in my limited experience with unity, wrap my head around how to make this calculation.
Take a look at this project. How do I get the bullets to shoot toward the cursor all the time? Keep in mind that the character isnt necessarily in the middle of the screen.
http://caleb-stamper.com/downloads/2DGameplayTutorial.zip
THANK YOU so much for helping.
For those who'd like a taste of my trouble before downloading:
var projectile: Rigidbody; var speed = 20; function Update () { if(Input.GetMouseButton (0) ) { var instantiatedProjectile : Rigidbody = Instantiate(projectile,transform.position, transform.rotation);
var mousex = Input.mousePosition.x;
var mousey = Input.mousePosition.y;
instantiatedProjectile.velocity = transform.TransformDirection(Vector3 (0,mousey - .5 * Screen.width,speed));
}
}
Answer by duck · Mar 14, 2010 at 08:40 PM
I have to say, I didn't download the project. But from the code you posted, something along these lines should do the job. The trick is - to read the current distance from the camera (to the player's ship, or whatever is launching the projectile). Then convert the mouse position to a world position at the same distance away from the camera.
You can then use the normalized difference between these two points as the velocity - multiplied by whatever speed you like.
(untested code - for illustrative purposes, may contain errors, etc!)
var projectile: Rigidbody; var projectileSpeed = 20; function Update () { if(Input.GetMouseButton (0) ) { var instantiatedProjectile : Rigidbody = Instantiate(projectile,transform.position, transform.rotation);
// get current distance from camera
var distFromCam = Camera.main.transform.InverseTransformPosition(transform).z;
// get target in world space, at same distance from camera:
var targetScreenPos = Input.mousePosition;
targetScreenPos.z = distFromCam;
var targetPosition = Camera.main.ScreenToWorldPoint(targetScreenPos);
// calculate direction & velocity:
var targetDelta = (transform.position - targetPosition);
var launchVelocity = targetDelta.normalized * projectileSpeed;
instantiatedProjectile.velocity = launchVelocity;
}
}
(If all your objects are always at the same fixed distance away from the camera, you could optimise this code by having that as a constant value rather than looked-up using InverseTransformPosition)
I get this when trying to use ducks code:
Assets/Scripts/ShooterScript.js(10,39): BCE0019: 'InverseTransformPosition' is not a member of 'UnityEngine.Camera'.
Ah yes, ok that line shoud be: Camera.main.transform.InverseTransformPosition(transform).z; (edited answer to show this too)
Sorry it took so long to respond. I was so excited about the solution I completely forgot to respond. The code was general but after working out the syntax and variable names this worked perfectly. Even the z axis was perfect. Thanks a LOT!
Could you please post the code you used (with the right syntax and variable names) for other users like me ? Thanks in advance.
Answer by sean · May 06, 2010 at 09:28 PM
I was poking around at the Camera class documentation the other day, and I believe it has built-in functions for turning a screen or viewport coordinate (e.g. a mouse x/y) into a world-space coordinate at a specified depth from camera, or better yet, a ray that works with the collision system.
Your answer
Follow this Question
Related Questions
How To Get Object To Aim at Mouse in Top Down 3D Shooter 4 Answers
Unity, Y U IGNOR MY Y? 2 Answers
Third-person mouse based-movement is blocked by edges of screen? 0 Answers
2D platformer cursor 1 Answer
Mouse Aim- Weapons 2 Answers