Projectile move towards mouse cursor

I’m not looking for answers on how to make my character face my mouse cursor, but just so the bullet coming from the character will move towards where my mouse cursor was when it shot. The game is currently a 2d top down shooter and all my bullets do is essentially act like bombs. The instantiate, but do not move.

What code I have is here.

function BulletShot(){
	var BulletClone = Instantiate(bullet, transform.position, Quaternion.identity);
	rigidbody.AddForce(transform.forward * BulletSpeed);
	Physics.IgnoreCollision(BulletClone.collider, collider);

}

You can create a plane and do a raycast against it to get the current mouse position in 3d space (at y = 0)

This is C#, shouldn’t be too hard to convert to JavaScript

if( Input.GetMouseDown( 0 ) )
{
    Plane zeroPlane = new Plane( Vector3.up, Vector3.zero );
    Ray ray = Camera.main.ScreenPointToRay( Input.mousePosition );
    float distance;

    if( zeroPlane.Raycast( ray, out distance ) )
    {
        Vector3 outputPosition = ray.origin + ray.direction * distance;
        Debug.Log( "Position: " + outputPosition  );

        //You can use this position to rotate the bullet like so
        BulletClone.transform.LookAt( outputPosition );
    }
}

If you want the resulting position at a different height than zero, change the Vector3.zero in the first line.

If you’re making a top down shooter with the camera set at some height and looking down to the ZX plane (rotation = 90,0,0), this script will shoot in the direction of the point the mouse is on. Don’t forget to uncheck Use Gravity in the rigidbody:

var bullet:GameObject;
var BulletSpeed:float = 20;

function BulletShot(){

	var myPos = transform.position;
	var dist = Camera.main.transform.position.y-myPos.y;
	var x = Input.mousePosition.x;
	var y = Input.mousePosition.y;
	var dir = Camera.main.ScreenToWorldPoint(Vector3(x, y, dist))-myPos;
    var BulletClone = Instantiate(bullet, myPos, Quaternion.LookRotation(dir));
    BulletClone.rigidbody.velocity = dir.normalized * BulletSpeed;
    Physics.IgnoreCollision(BulletClone.collider, collider);
	Destroy(BulletClone,5);
}

function Update(){

	transform.Rotate(0,Input.GetAxis("Horizontal")*60*Time.deltaTime,0);
	transform.Translate(0,0,Input.GetAxis("Vertical")*10*Time.deltaTime);
	if (Input.GetButtonDown("Fire1")){
		BulletShot();
	}
}

It also included the movimentation instructions, but you can replace them with your own code.

The problem with your code is this line here:

rigidbody.AddForce(transform.forward * BulletSpeed);

rigidbody (unless it was explicitly overridden) is the access variable to the rigidbody of the game object to which your script is attached to. Since your bullet is being instantiated from within this script, then it’s quite obvious that this script is not attached to your bullet – and even if were, the function would have to be called again for rigidbody to start referring to the bullet’s own rigidbody, in which case you’d have a second bullet with the same problem. You need to access the rigidbody of the bullet that you just instantiated, not the rigidbody of the bullet instancer.

So you basically want something like this:

BulletClone.rigidbody.AddForce(transform.forward * BulletSpeed);

But that doesn’t solve all your problems.

You see, that line is inside the same function which instantiates the bullet, which means that it gets called after the bullet is instantiated and never again (not for the same bullet). And since you didn’t pass a ForceMode parameter, then that will be set to the default value of ForceMode.Force. There’s nothing wrong with that, but I’m thinking that’s not what you want. Considering that ForceMode.Force was meant to be used so that force can be applied over several frames, I’m 100% sure that that’s not what you want. What you actually want to do is apply an initial velocity to the bullet, which you do by passing either ForceMode.Impulse or ForceMode.VelocityChange as the ForceMode parameter.

As for the issue of wanting the character to face the cursor, the others have already addressed that. I haven’t perused the actual code which they posted, but I’m too lazy to do so, so let’s just say that I trust them. XD