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
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
– slayer29179I 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
– slayer29179Since 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.
– aldonalettoYAY!! it works :D thank you Aldonaletto, it was the plane method I I was trying to click on nothing :)
– slayer29179Hey guys, is it possible for someone to convert this into C#, please?
– Alter