Unity - Get Local Mouse Position from the center of the Gameobject clicked on

by gameOne   Last Updated April 27, 2015 21:05 PM - source

I want to get the mouse clicked position from the center of the gameobject, say a Sphere of Scale(1, 1, 1), for instance. If I click on the center of the sphere it should return the x component as zero, on clicking to the extreme left of the sphere, it should return -0.5 as the x component of the vector3 and 0.5 on clicking the extreme right of the sphere. The below code helps me achieve this when at origin. However, there is one constraint for it. The Sphere has to be positioned at (0, anything, anything) (as I am concerned with the x axis). Any help on how can I achieve this regardless of the Sphere position?

bool isGameOver = false;
float pointX;

// Update is called once per frame
void Update () {
        RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if(Physics.Raycast(ray, out hit)){
                if(hit.transform.tag =="Ball"){
                    pointX = transform.InverseTransformPoint(hit.point).x;

Related Questions

Raycast Masks not working Unity3D

Updated April 07, 2015 18:05 PM

Raycasting on Z in 2D

Updated April 28, 2015 21:05 PM