Shoot towards Mouse cursor 2D Scrolling Javascript

Hi I would appreciate some assistance

var prefabBullet:Transform;
var shootForce:float;
function Update()
{
	if(Input.GetMouseButtonDown(0))
	{
var instanceBullet = Instantiate(prefabBullet, transform.position, transform.rotation);

instanceBullet.rigidbody.AddForce(transform.forward * 400 *shootForce);
}

Debug.Log("MouseX"+Input.mousePosition);	// Mouse position on screen
}

I cant understand how to manipulate the mouse in relation to the force forward (it doesnt accept changing the transform.forward to Input.mouseposition)

Could someone help

Thanks

If I understand correctly, you are trying to shoot from a character toward the mouse. Transform.forward doesn’t have anything to do with that. You need to find the vector (character pos → mouse), with both value in the same space, screen, viewport or world, as long as it is the same. Better make it in world though, so you don’t have to convert it for the AddForce.

About finding the mouse position in 3D, look over there.

Thanks for your reply.

I am indeed trying to fire from a player to the mouse X and Y position - Side scrolling shooter with the ability to shoot 360 from the player(not worried about from the gun atm just from a cube)

I think I understand what your getting at.

I attempted this from what you have said but it doesnt seem to be working.

I made a new script, put it on the main camera as putting it on the player was not registering the raycast line on scene view.

function Update () {
// Draws a line in the scene view going through a point 200 pixels
// from the lower-left corner of the screen

    var ray : Ray = camera.ScreenPointToRay (Vector3(Input.GetAxis ("Mouse X"),Input.GetAxis ("Mouse Y"),0));
    Debug.DrawRay (ray.origin, ray.direction * 200, Color.green);
}

In terms of the world, what do you mean - do I assign the camera as the ‘world’ or have I got the wrong idea here?

I have this script attached to my player.

var prefabBullet:Transform;
var shootForce:float;

function Update()
{
	if(Input.GetMouseButtonDown(0))
	{
var instanceBullet = Instantiate(prefabBullet, transform.position, transform.rotation);
instanceBullet.rigidbody.AddForce(transform.forward * 400 *shootForce);
}

From what you said do I assign the parameter for (Vector3(Input.GetAxis (“Mouse X”),Input.GetAxis (“Mouse Y”),0)); to instanceBullet.rigidbody.AddForce ?

Cheers again

ok now what ive got is this :

var crosshairTexture : Texture2D;
var prefabBullet:Transform;
var shootForce:float;
var projectile: Rigidbody;
var speed = 20;

Screen.showCursor = false;

function OnGUI () {
    var mousePos = Event.current.mousePosition;
    GUI.DrawTexture( Rect( mousePos.x - (crosshairTexture.width/2),                        mousePos.y - (crosshairTexture.height/2),crosshairTexture.width,                         crosshairTexture.height), crosshairTexture);
}


function Update () 
{
    var mousePos = Event.current.mousePosition;

if(Input.GetMouseButton (0) )
    {
        var instantiatedProjectile : Rigidbody = Instantiate(projectile,transform.position, transform.rotation); 

        instantiatedProjectile.velocity = transform.TransformDirection(Vector3 (mousePos.x,mousePos.y, 0));
    }
}

From what you said previously Berenger, Ive now got a projectile which finds the mouse x and y vector points. Its attached to a GUITexture cross hair. It should shoot to the mouse x and y and 0 on z axis however, it isnt doing so :confused:

Please could you point me in the right direction with this (no pun intended :P)

Cheers

Ohh think im a bit closer still not working though.

var crosshairTexture : Texture2D;
Screen.showCursor = false;

function OnGUI () {

    var mousePos = Event.current.mousePosition;
    GUI.DrawTexture( Rect( mousePos.x - (crosshairTexture.width/2),
                           mousePos.y - (crosshairTexture.height/2),
                           crosshairTexture.width,
                           crosshairTexture.height), crosshairTexture);
}



var prefabBullet:Transform;
var shootForce:float;
var distance : float = 32.7518;
var projectile: Rigidbody;
var speed = 20;

function Update () 
{
var dir = transform.TransformDirection(Vector3(Input.mousePosition.x,Input.mousePosition.y,0));
var hit : RaycastHit;
Debug.DrawRay(transform.position, dir*distance, Color.yellow);

if(Input.GetMouseButtonDown(0))
	{
var instanceBullet = Instantiate(prefabBullet, transform.position, transform.rotation);

instanceBullet.rigidbody.AddForce (Vector3(Input.mousePosition.x,Input.mousePosition.y,0));

	}
}
  • How would I make it the players
    position so that the raycast follows
    the player
  • The raycast does not follow
    360degrees it only registers with 0
    to 180 deg ?