Fire bullet to mouse position in 3d space

Hey all, i am currently trying to detect the mouse position and fire my bullet towards it, i am not 100% sure it can actually be done (i really hope it can) a good reference would be some space fighting games such as Freelancer, i have had a look at Ray-cast which i don’t understand but i tried some scripts i found off here, sadly the ones i looked at were for 2d games such as side scrollers, here is the code I am using currently;

var character : Transform; //main character
var bullet : Transform; // the bullet prefab

function Update()
{	
	var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
	var hit : RaycastHit;
	if (Physics.Raycast (ray, hit, 100)) 
	{
	  // Now you have where your player is and a 3D point the mouse is over
	  Debug.DrawLine (character.position, hit.point);
	}

	if(Input.GetMouseButtonDown(1))
	{
		var projectile = Instantiate(bullet, GameObject.Find("oneSpawn").transform.position, Quaternion.identity); 
		projectile.transform.LookAt(hit.point); 
		projectile.rigidbody.velocity = projectile.transform.forward * 10;
	}
}

I am finding it hard to try and explain what I want but I don’t want it to be as simple as they just click on the enemy’s to fire, but where ever the mouse cursor is the bullets will fire forward towards it

thank you to anyone that can solve my problem because I don’t know what to search for if I don’t believe its even possible

1 Answer

1

This code should work! Are you having any problem? I would only save the oneSpawn reference in a variable, and do the raycast only when if the button is pressed - both changes related to performance improvement:

var character : Transform; //main character
var bullet : Transform; // the bullet prefab
private var spawnPt : GameObject; // holds the spawn point object

function Update(){   
  if(Input.GetMouseButtonDown(1)){ // only do anything when the button is pressed:
    var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    var hit : RaycastHit;
    if (Physics.Raycast (ray, hit, 100)){
      Debug.DrawLine (character.position, hit.point);
      // cache oneSpawn object in spawnPt, if not cached yet
      if (!spawnPt) spawPt = GameObject.Find("oneSpawn");
      var projectile = Instantiate(bullet, spawnPt.transform.position, Quaternion.identity); 
      // turn the projectile to hit.point
      projectile.transform.LookAt(hit.point); 
      // accelerate it
      projectile.rigidbody.velocity = projectile.transform.forward * 10;
    }
  }
}

thanks Aldonaletto for trying but still nothing, sorry for not mentioning the problem, it won't spawn, won't fire, nothing just nothing happands. My previous script which spawned a bullet in front of the ship and sent it forward worked but wasn't what I needed, the character has a controller on, the bullet is in the prefab working with a trigger, I have equipped the script and all the variables, if it helps

I added some debug notes to see where it gets and it won't go any further than the if (Physics.Raycast (ray, hit, 100)){ then the debugging stops

Since you said ship, I suspect that you're clicking the empty space - and this won't work, because Raycast only returns true if some collider (at 100m or less, in this case) is hit. If this is the case, a possible hack is to create a big plane (scale 10000,1,10000) in front of the ship at a big distance (5000, for instance) and delete its mesh renderer. Child the plane to the player, and remeber also to change the raycast to Physics.Raycast(ray, hit) to remove the distance limit.

YAY!! it works :D thank you Aldonaletto, it was the plane method I I was trying to click on nothing :)

Hey guys, is it possible for someone to convert this into C#, please?