- Home /
Question by
Akabuu · Apr 21, 2016 at 09:59 PM ·
positionmousepositionpositioningmouseclick
Check mouse click in front of object
I have a boat, the boat has cannons on both sides. Cannons are prefabs. I need a script that checks if the mouse click (fire cannons) is in front of the cannon or not, so that it only fires on the side where the mouse is in front of the cannons. The game is top-down, if that matters.
This is what i got so far but it doesn't collect the position data.
var partSys : ParticleSystem;
var mpox = 0.0;
var gpox = 0.0;
function Start () {
//temporary particle system to test fire
partSys = gameObject.GetComponent(ParticleSystem);
}
function Update () {
//for debug purposes
mpox = Input.mousePosition.x;
gpox = transform.position.x;
// Tried like this but still nothing
// mpox = Camera.main.ScreenToWorldPoint(Input.mousePosition).x;
// gpox = Camera.main.ScreenToWorldPoint(transform.position).x;
//this is where i'd do the checking, but none of the "position.x" gets anything
//also no errors, just plain zeros
//it fires the cannons on both sides
if(Input.GetMouseButtonDown(0) && Mathf.Sign(gpox * mpox) == Mathf.Sign(gpox)){
partSys.Emit(1);
}
}
Comment
Your answer
Follow this Question
Related Questions
How do I get mouse position in game units? 3 Answers
How to place an object near N number of objects in order to avoid overlap? 0 Answers
Move object with Translate on X axis (with mouse) 0 Answers
Offset position to a target 3 Answers
[CODE]Keep Gameobject position exactly at mouse cursor when cast float position to int 1 Answer