This question was
closed Apr 09, 2016 at 12:02 PM by
callme for the following reason:
The question is answered, right answer was accepted
Question by
callme · Apr 08, 2016 at 09:24 PM ·
c#raycast3dtransform.positionmouse position
[SOLVED] get object which is nearby to mouse
On the 3D scene 15 objects moving right. How to get object position when mouse on left side and height of this object? (see screnshot below) My idea is put trigger and use onMouseOver but its looks like dirty way, any better suggestions?
//sorry if wrong english http://imgur.com/W2hOpbI
Comment
Best Answer
Answer by b1gry4n · Apr 08, 2016 at 11:15 PM
http://docs.unity3d.com/ScriptReference/Input-mousePosition.html
http://docs.unity3d.com/ScriptReference/Camera.WorldToScreenPoint.html
Get both objects in screen space and compare.
if (mousePointerScreenSpace.x > objectScreenSpace.x){
//mouse is to the right
}
Thank for answer. This is looks simple and works good. This is my gameObject code
Vector2 mousePos = Input.mousePosition;
Vector2 pos = Camera.main.WorldToScreenPoint(tf.position);
//defining area
float rBound = tf.position.x - xBound;
float topBound = tf.position.y + yBound;
float botBound = tf.position.y - yBound;
//transform area to screen space
Vector2 TopRight = Camera.main.WorldToScreenPoint(new Vector3(rBound, topBound, tf.position.z));
Vector2 LeftBott = Camera.main.WorldToScreenPoint(new Vector3(tf.position.x, botBound, tf.position.z));
//if cursor on the area
if (LeftBott.x < mousePos.x && mousePos.x < TopRight.x && LeftBott.y < mousePos.y && mousePos.y < TopRight.y){
//do something
}