Hello,
I am trying to calculate the bounds of a TextMesh so I can make it clickable and so far I have failed.
I have used all the possible combinations of renderer.bounds and none of them seems not even roughly accurate.
I even debug using gismos to see the result on screen with:
Vector3 center = renderer.bounds.center;
Gizmos.color = Color.white;
Gizmos.DrawWireCube(center, renderer.bounds.extents);
Gizmos.color = Color.yellow;
Gizmos.DrawWireCube(center, renderer.bounds.size);
Gizmos.color = Color.red;
Gizmos.DrawWireCube(center, renderer.bounds.min);
Gizmos.color = Color.green;
Gizmos.DrawWireCube(center, renderer.bounds.max);
and the yellow one looks like the best one but how can I use it… If we assume one of the above is OnScreenSize…
Vector3 touchPosition = Input.mousePosition;
Vector3 screenPosition = Camera.mainCamera.WorldToScreenPoint( transform.position );
if( screenPosition.x - ( OnScreenSize.x ) < touchPosition.x screenPosition.x + ( OnScreenSize.x ) > touchPosition.x screenPosition.y - ( OnScreenSize.y ) < touchPosition.y screenPosition.y + ( OnScreenSize.y ) > touchPosition.y )
{
isClicked = true;
}
If you want to make clickable object, there is a better way. You could use Physics.Raycast method.
See a little example from my project bellow:
void OnGUI()
{
Vector3 pos = Input.mousePosition;
if (Camera.current != null)
{
Ray ray = Camera.current.ScreenPointToRay(pos);
RaycastHit hit = new RaycastHit();
if (Physics.Raycast(ray, out hit))
{
#if UNITY_EDITOR
Debug.DrawRay(ray.origin, ray.direction * 25, Color.blue);
#endif
bool leftMouseButton = Input.GetMouseButton(0);
bool rightMouseButton = Input.GetMouseButton(1);
bool middleMouseButton = Input.GetMouseButton(2);
if (leftMouseButton | rightMouseButton | middleMouseButton)
{
GameObject hitObject = hit.collider.gameObject;
if (hitObject != null)
{
//Debug.Log("hit the "+hitObject.name);
if (leftMouseButton)
OnObjectLeftClicked(hitObject);
if (rightMouseButton)
OnObjectRightClicked(hitObject);
if (middleMouseButton)
OnObjectMiddleButtonClicked(hitObject);
}
}
}
}
}
Yeah but then I’ll have to create a collider for each textmesh
Any other methods anyone?
Patico
May 29, 2013, 11:45am
5
I think, there are not other methods.
Why you don’t want to create colliders?
What the collider creates for a TextMesh is essentially the same as renderer.bounds.size but it has to use the physics engine which is 1. unreliable in some cases 2. heavy so I am wondering how to actually do it using just those bounds instead but they seem to lose the proper center for some reason I have yet to understand