When GUI enters object... how to?

Hi again.

How can I check if a GUI entered an object?

See the images attached. I want to do something when the center circle (GUI Texture) reaches the white box.
And also, when the circle is within the box AND the user clicks the mouse. How? Where in the reference I can get such info?

The objective here is to make the center GUI act as the mouse pointer. There is some example around this?

Thx.

Anyone? Please? :cry:

“http://answers.unity3d.com/questions/11850/gui-with-own-mouse”?

Just used Unity Search - “Programmable Search Engine”

And, the solution here is just to draw the Gui where the mouse is located, and do all other function as if it were mouse functions.

Raycasting to see if your cursor is over something can also be found with the Google Unity Search… Keywords - “Raycast mouse” turned up this - “http://forum.unity3d.com/viewtopic.php?p=260626”

Those combined should solve your problems. Read them, check docs if you have functionality questions, and use the search! Let me know how it goes :wink:

Just a guess, but you could try something like taking the screen coords of the box and the coords of the gui and compare them to see if they intersect each other.

Although it might be easier, and less resource heavy, to send a ray from the center of the player camera out into the world, and when it intersects with the white box allow the user to interact with it.

Might work even better with a cylinder that doesn’t render attached to the camera (also allows interaction only to the maximum length of the cylinder). This also allows you to overcome the ray’s problem of only working at the exact center of the screen.

Just some ideas, hope they help.

Oh, thank you guys :slight_smile:

Just took a look at the raycast method and it seems pretty nice!

Using that, I’ll make a cylinder FIXED at screen center as said here and Raycast it.

I’ll post the results and other questions here, ok?

Ty :slight_smile:

Its kinda hard actually :frowning:
I thought it was going to be a lot easier.

I created a cylinder as a child of the First Person Controler’s Camera. But the cylinder’s scale proportions depends on the screen resolution, and the GUI (gray circle) doesn’t.
Also, how can I know where is the FORWARD point of the cylinder? I need to raycast to the FORWARD position of the cylinder, right?

How can I overcome both situations?

I really don’t know how to keep the same size between the GUI circle and the cynlinder. And also don’t know how to raycast exactly forward (the top base) of the cylinder.

if it doesn’t have to completely precise, you could scale the cylinder based on the screen width, getting lower as the resolution gets higher… This is kind of a hit miss approach, but it should be a linear progression (i think) so once you get it you’re good. (Try starting with your smallest used screen size and check how big the cylinder needs to be, then the largest possible screen size and how big the cylinder needs to be, then interpolate between the two depending on the player’s screen)

for the other, i wouldn’t use raycast if you use a cylinder. no real purpose for it if you add a collider to the cylinder and make it long enough to hit whatever objects you’ll want to hit. As long as the cylinder’s a child (and directly at the center of the camera) it’ll act like a make-shift raycast. maybe.

Now I’m curious about the use of position.forward x)

I’m trying to draw a line between the start of the object and it’s front.

I’m doing this:

	var fwd = transform.TransformDirection (Vector3.forward);
	Debug.DrawLine (transform.position, fwd);
	Debug.Log(fwd);

And the line stills going to the wrong direction. It gets the “fwd” variable as (0, 1.0, 0). What am I missing here?

About the suggestion on doing the cylinder instead of the raycast, I like that, but waht about when the collider gets 2 things in the way at the same time (yes… that will occur with the raycast as well)? What should I do? Get the near one? :stuck_out_tongue:

Also, I got very curious about the draw line to the front of a object… gona stick to that for this one.

Thanks :slight_smile:

First off, transform.forward is in world space, hence your problem. You’re getting the forward direction and then transforming it to local, which will always be (0,0,1). If you use the cylinder solution, yes you would just compare collider distances from you. Whichever was closer. Although, if they overlap, or if one has a huge collider but it’s center is further back, etc, things could get complicated. Try that though, and see how it works.

Hi there.
I was a little away, but I’m with Unity again.

So… after a lot of tests, I still don’t know how to raycast ALWAYS to the same direction (e.g. forward).

I need to do some calcs with the transform.eulerAngles?

no just use transform.forward

But transform.forward gives me something like (0.5, -0.4, 0.3) or (0.0, 1.0, 0.0).

When I do Debug.DrawLine(transform.position, transform.forward) I keep getting the line starting in the object and ending in the position 0,0,0 of the world.

I think I really didn’t understood the transform.forward… :frowning:

How do I do it?

Thx

transform.forward is a direction.
you add it to your current position to get a ray forward from your current position

Thank you! :slight_smile: :slight_smile: :slight_smile:

Now I understand how it works.

Going to continue my tests and come back here with results or more questions :wink:

Thx again

I still trapped here :frowning:

private var oldColor : Color;
var target_distance : float = 10.0;
var layer_interactive_obj : int = 9;

function Update () {
	var _target = (transform.position + (transform.forward * target_distance));
	Debug.DrawLine (transform.position,_target);
	
	var hit : RaycastHit;
	
	if (Physics.Raycast (transform.position, _target, hit, target_distance)) {
		Debug.Log("[" + hit.collider.gameObject.layer + "] " + hit.collider.gameObject.name);
		
		if (renderer.material.color != Color(1,0,0,1))
		{
			oldColor = renderer.material.color;
		}
		hit.collider.gameObject.renderer.material.color += Color(0.01,0,0);
		
		if (hit.collider.gameObject.layer == layer_interactive_obj)
		{
			renderer.material.color = Color(1,0,0,1);
		}
	}
	else
	{
		if (oldColor != new Color())
			renderer.material.color = oldColor;
		print("0");
	}
}

What am I missing here? My raycast NEVER detects ANYTHING besides one object…

I created a cube (default layer) in front of the raycast line and it just ignores it.

What is the output in the console, from your Debug.Log ?

Why not just do it the easy way? From what you write in your first post you want the circle in the center to be you mouse pointer and from that I take it that it is alway in center?

then you should just:

//some manger script or on controller
void Start()
{
Screen.lockCursor = true;
Screen.showCursor = false;
}
....
//On object you want to push -> Add collider -> Add this method triggered by Monobehaviours when a collider i attached
void OnMouseUp()
{
OpenDoor();
}

Actually i made it work. I was usin the raycadt directin in the wrong way…

Slem, I want something like that, but I want to make the circle change its color when passing throught a interactive object. And the mouse click should only work when the circle its above something.

Thx :slight_smile:

Actually, this will infact do just that. And if you add void OnMouseEnter and OnMouseExit you can change the color of the circle. You will find more info in the documentation.

Hmmmm! Didn’t know that!

Very good indeed.

I have just one observation about that: sometimes the mouse don’t start at the middle of the screen.
Why is that?

(tried on the unity preview, not with compiled versions)

Thank you