- Home /
Duplicate Question
Can't shoot towards mouse click point
I'm making 2d space game from side-view, and i wanted to add some weapons that would shoot on x and y axis towards the point where the player click the mouse.I found something so i tried this:
var hit : RaycastHit ;
if(Input .GetButtonDown ("Fire1"))
{
var ray : Ray = Camera .main .ScreenPointToRay (Input .mousePosition );
if(Physics.Raycast (ray, hit, Mathf.Infinity))
{
var shootProjectile : Rigidbody = Instantiate(projectile, GameObject.Find("spawnPoint").transform.position, Quaternion.identity);
shootProjectile.AddForce (hit.point * projectileSpeed);
}
}
}
It shoots well if i click in to empty space, but when i click on object with collider, it shoots in very weird directions...I spend day and half finding a solution, but I didn't find it. I tried many things, just do'nt work. Can anyone help me? The best would be to post repaired code and explain why my code don't work :)
Follow this Question
Related Questions
Can't shoot towards mouse click point 1 Answer
click shooting how do i do it? 1 Answer
Display sprite where clicked on screen. 1 Answer
Making a object move to mouse point? 0 Answers
Converting button to mouse click 1 Answer