I have a sphere which rotates around the box.
The sphere looks at the box with LookAt and casts a Ray with Raycast, transform.forward.
The lenght of the ray is 10 meters.
What I want to do is that when I drag the sphere from the box further than 10 meters (i.e. my ray no longer hits the box) the sphere should move no further (however I shall still be able to rotate it around boxes “orbit”).
As I understand what I need to do is find the coordinates of ray`s origin while the ray is about to leave the box and give the sphere the coordinates of rays origin.
How should I do it? Or maybe there`s an easier way?
Here is my code if any:
void OnMouseDrag () {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
transform.position = new Vector3(ray.GetPoint (9).x, transform.position.y, ray.GetPoint (9).z);// I`m moving the sphere with the mouse;
Physics.Raycast (transform.position, transform.forward, 10);
Debug.DrawRay(transform.position, transform.forward * 10, Color.red);