- Home /
NGUI and raycast hit point
I'm trying to create a script to move my character to a point on the terrain that works with NGUI because currently I am using a script along with NGUI and it is causing me issues.
void rayCastClick()
{
if (Input.GetButtonDown("Mouse0"))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray, out hit, 100))
{
if(hit.collider.gameObject.tag == "Terrain")
{
//Take hit point and move to hit position on terrain
}
}
}
}
}
Can anyone give me an example of how I would do this using NGUI's event system? I'm not sure how to do it.
Could you clarify how NGUI plays into moving your character? NGUI is for buttons, lists, etc, but doesn't really do anything beyond that (i.e., its not for moving scene objects around).
Are you wondering how to get a button to tell something to 'DO YOUR THING'?
NGUI uses raycasts apparently so me making my own raycasts messes with NGUIs ones. I just don't understand how to use NGUI to do the same thing.
I want the point on screen I click to fire a ray that checks whether the terrain is hit and moves the player towards the vector of where the ray intersects the terrain.
Answer by whydoidoit · Jun 18, 2013 at 06:00 AM
NGUI uses raycasts but your raycasts are not affecting it - it doesn't work like that. I would leave it alone as @iwaldrop says because your UI should be on a different layer to everything else (and your UI should normally be positioned away from your scene).
If you do want to access the last thing hit by NGUI it's in:
UICamera.lastHit
$$anonymous$$y raycasts go through buttons and hit the terrain, which I don't want.
Technically that isn't happening, but I understand what you mean - visually it appears to happen. I would not send your raycast if UICamera.hoveredObject != null.
Answer by Andres-Fernandez · Feb 20, 2014 at 07:51 AM
You have to attach the UICamera script to your main camera object (the one that shows the terrain)and set the Event type to UI/World (I can't remember which one, but I guess it is World). That way, objects shown by that camera will also generate the NGUI events.
Answer by chaurasiyapawan · Nov 13, 2014 at 07:46 AM
May be you can use this
static bool UICamera.IsPressed ( GameObject go)