- Home /
Can't shoot towards mouse click point
I'm making 3d 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 :)
Although your game is 2D, the colliders are 3D, so you are hitting the collider at a position that is above the 2D game plane. To fix this, lower all your colliders so the top of the collider is at the same y-value as your game plane.
i.e. if your 2D is on the X-Z plane, then all your colliders and gameObjects have to be positioned on the same Y value.
I'm stupid... I didn't say that the game is 3d, just from side-view..
then it could be how you calculate the AddForce. You should also store a local reference to the spawnpoint (at start if the spawnpoint doesn't change).
var spawnPt : GameObject;
function Start()
{
spawnPt = GameObject.Find("spawnPoint");
}
Try
shootProjectile.AddForce( (hit.point - spawnPt.transform.position) * projectileSpeed );
It works!!! BUT still when i shoot on object with collider, it is veeery slow otherwise not. so I changed it to this:
shootProjectile.AddForce(( hit.point - spawnPt.transform.position )*projectileSpeed);
Now it shoot fast on objects with collider, but(and i don't know why) when i click on something without collider, it shoots incredibly fast..
I just fixed the line :
shootProjectile.AddForce( (hit.point - spawnPt.transform.position) * projectileSpeed );
I havn't tested this , but the idea is to find the direction to the collider from the spawnPoint. Just adding force at the position of the collider will give the same result no matter where the collider is.
Answer by Arlei · Aug 19, 2012 at 08:29 AM
Okay, I fixed it!
var projectile : Rigidbody;
var projectileSpeed : float;
var spawnPt : GameObject;
function Start()
{
spawnPt = GameObject.Find("spawnPoint");
}
function Update()
{
var hit : RaycastHit ;
if(Input .GetButtonDown ("Fire1"))
{
var ray : Ray = Camera .main .ScreenPointToRay (Input .mousePosition );
if(Physics.Raycast (ray, hit, Mathf.Infinity))
{
projectile = Instantiate(projectile, GameObject.Find("spawnPoint").transform.position, Quaternion.identity);
projectile.AddForce(( hit.point - spawnPt.transform.position )*projectileSpeed);
}
}
}
function FixedUpdate()
{
var velocity = projectile.velocity;
if (velocity == Vector3.zero) return;
var magnitude = velocity.magnitude;
if (magnitude > projectileSpeed || magnitude < projectileSpeed)
{
velocity *= (projectileSpeed / magnitude);
projectile.velocity = velocity;
}
}
I'm so happy! :D Who have the same problem, you can use this code :) thx for help AlucardJ :)
I know it old ! But your code has an error! RaycastHit hit variable haven't been assigned