Hello, I am making a 2D shooter and I have a question about firing a bullet:
When I click the mouse, I want to move an object outwards from point A in the direction of point B at a set velocity. I want point B to be wherever the mouse is clicked on the screen, how can that be done.
I have tried this;
var projectile : Rigidbody;
var speed = 2;
function Update()
{
if( Input.GetButtonDown( "Fire1" ) )
{
var instantiatedProjectile : Rigidbody = Instantiate(
projectile, transform.position, transform.rotation );
instantiatedProjectile.velocity =
transform.TransformDirection
(Event.mousePosition);
}
}
But using this just makes the object move in a weird direction (sometimes even at the camera) I have also tried to work in “Input.mousePosition” but I cannot get that to work. Does anyone have any code that would fix my script(or another script that would accomplish what I want to do)? Thank you.
I haven’t worked with mouse position before, but my guess, is that the mouseposition is relative to the camera, and not your character.
Mouse position is the pixel coordinate of the mouse on the screen… i.e. useless for this.
Here is a script I wrote that is similar to what you wanted.
The important bit is that we get the mouse point as a ray (point in space with direction) - and then we have to figure out where this is in real space - either by arbitrarily picking a point (50 units in-front of me), or by using some other criteria (collides with game-object).
var targetRange : float = 50.0;
var turnSpeed : float = 6.0;
var _parent : Transform;
function LateUpdate(){
var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Input.mousePosition.x,Input.mousePosition.y,0));
var rayHit : RaycastHit;
var rotateTo : Quaternion;
Debug.DrawRay(ray.origin, ray.direction * targetRange, Color.yellow);
if(Physics.Raycast(ray, rayHit, targetRange) rayHit.transform != _parent ){
rotateTo = Quaternion.LookRotation(rayHit.transform.position - transform.position, transform.up);
}else{
rotateTo = Quaternion.LookRotation(ray.GetPoint(targetRange) - transform.position, transform.up);
}
transform.rotation = Quaternion.Slerp(transform.rotation, rotateTo, Time.deltaTime * turnSpeed);
}
Thank you for your help, but I am still somewhat lost. If I understand your script correctly, it is supposed to take wherever I click on the screen and draw a line from parent object to that point. I added the script to the scenes camera and nothing noticeable happened (I had gizmos turned on in unity). What am I doing wrong?
I forgot to mention - that’s a turret script - so it aims (with the rotateTo) what ever its applied to to the point in space.
That line only appears within the scene window, and only when it is running 
I now rewrote it to show off it’s full functionality far more clearly - just open the project and run it with Gizmo’s on as you did before.
Being a 2D shooter you may get off easily. If you’ve got it so that ‘z’ is depth, then all you should need to do is go:
//untested
target : Vector3;
function Update(){
//get the ray
var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Input.mousePosition.x,Input.mousePosition.y,0));
//Since there is no 3d angle - the x and y cords are going to be the same no matter how far you shoot it - so why bother?
target = ray.origin;
//get rid of the pesky z (assumes that your game runs on the z = 0 plane thingy).
target.z = 0;
/// if you wanted, you could get rid of the 'ray' altogether, but it makes for nice reading/demonstration.
}
429499–14885–$mouseAim.zip (43.6 KB)