Get click position on gameobject from OnPointerClicks PointerEventData
Hello,
my question is how to get the click position on a gameobject after the onpointerclick handler fired. I know how to receive the worldposition, but the problem is, that I can rotate and move the gameobject. With it, its local coordinate system will move and rotate which I need to access to get the exact position of my click on the gameobject.
I was searching for a similar case all over the internet but could not find anything close. I think the right spot to look at is the Raycast but I have no clue how to use it..
Edit: With the RayCastResult from pointerCurrentRaycast I am only able to receive the worldPosition, not the position on the gameobjects coordinate system.
I would be thankful for help,
Erik,
Answer by osirisStuttgart · Mar 29, 2018 at 03:44 PM
I solved it by adding a RectTransform instead of a Transform
var blabla= new GameObject("Blaa",typeof(RectTransform));
and calling this:
public void OnPointerClick(PointerEventData eventData)
{
Vector2 clickPosition;
if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(GetComponent<RectTransform>(),eventData.position, eventData.pressEventCamera, out clickPosition))
return;
print("clicked position on the gameobject is :" + clickPosition.x);
}