Cosmas
February 6, 2012, 5:34am
1
Object 1: Mesh Sphere with a Sphere Collider set to Trigger
Contains script:
void OnTriggerEnter()
{
Debug.Log("Do Something");
}
void OnTriggerExit()
{
Debug.Log("Do Something Else");
}
Object 2: Camera
Contains script:
void Update()
{
RaycastHit hitObj;
Ray ayanami = Camera.main.ScreenPointToRay(Input.mousePosition);
bool didHit = Physics.Raycast(ayanami, out hitObj);
if(didHit)
{
Debug.Log("I hit: " + hitObj.collider.gameObject.name);
}
}
This is the simplest implementation possible but I’m getting nothing back from the sphere collider. Is there some ongoing bug with OnTriggerEnter or am I completely using this wrong?
OnTriggerEnter won’t execute if a Ray touches the sphere. If you need that behaviour, you could do this:
void OnRayHit(Vector3 point)
{
Debug.Log("I was hit by a ray at: "+point);
}
void Update()
{
RaycastHit hitInfo;
Ray cameraRay = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(cameraRay , out hitInfo))
{
Debug.Log("I hit: " + hitInfo.collider.gameObject.name);
hitInfo.collider.gameObject.SendMessage("OnRayHit", hitInfo.point);
}
}
Also, a Ray stops when it hits something, so you can’t get information on where it would have come out, at least not as easily.
Cosmas
February 6, 2012, 2:30pm
3
Also, a Ray stops when it hits something, so you can’t get information on where it would have come out, at least not as easily.
That’s ultimately what I need though. Its a hover selection script in 3d space. I’ll try it though and see if I can make it work. Thanks f.cherchi
Edit: I’m so stupid, I found a workaround. Monobehavior.OnMouseEnter and OnMouseExit work perfectly on an object with a Raycast.
Cosmas
February 7, 2012, 4:18am
4
Does anyone know how I can achieve a smooth progressive fade from this point?
void OnMouseEnter()
{
if(navitasPlayer.Instance.mode == "Sector" selected == 0)
{
LerpColor(off, on, 1.0f);
selected = 1;
}
}
void OnMouseExit()
{
if(selected == 1)
{
LerpColor(on, off, 1.0f);
//current = Color.Lerp(on, off, 10.0f);
//renderer.material.color = current;
selected = 0;
}
}
void LerpColor(Color startCol, Color endCol, float time)
{
for(float i = 0.0f; i < 255.0f; i++)
{
current = Color.Lerp(startCol,endCol,time);
renderer.material.color = current;
}
}