# Getting position of a coordinate on the surface of a sphere

So I want to create a object B close to another object A, but I'm trying to avoid that A covers B:

I have thinking how to achieve that. Take a look of the following drawing:

I have been thinking in a solution in a math - geometry way before searching for some Unity3D function to solve the problem. I have realise A and B would be on the same surface if the camera is within a sphere with radius c-A. So I guess the solution can be related to get a point at a B at a distance A-B from A on the surface of the sphere with radius c-A. Does it has sense? Any other idea? How to do it with maths and Unity?

• If you use spherical coordinates instead of cartesian, this kind of comparison is trivial. – ddyer Apr 14 '18 at 0:55
• @ddyer it was easy to find a function to do it in Unity3D. Unfortunately I don't see it as trivial as you! :/ – chelder Apr 14 '18 at 21:41

Well first of all, is the arrow a vector or just your own way of pointing? Particle A minus B doesn't produce what's on the second image. A minus B can mean several things depending on perspective! If A and B are POINTS in space, then subtracting them will give you the distance or displacement between them (scalar or vector). But if instead you view A and B as Velocity vectors, then your experiment changes. For an easy fix, when the object collides with the other, examine the variable 'closestPoint'. Its in there somewhere. No guarantees, but worth a shot.

• I think there is a big misunderstanding here. I have not drawn an arrow but what the camera is "seeing". So in both cases the center of the camera "sees" the point A. In the first example it cannot see B (because A is hiding it). In the second example, the camera con see B at the right. – chelder Apr 14 '18 at 21:23

Create object B on the center of the camera

Transform B = Instantiate(prefabB, camera.position, Quaternion.Identity);


Move it's local position to object A

B.localPosition = A.position;


Rotate object B by some angles, this will cause it to rotate around camera, so it will be on the same distance as A but in a different angle

B.Rotate( new Vector3(0, 30.0f, 0) );


This assumes that the circle is on the x-z axis and the perpendicular axis is the y-axis.

• hmm I'm not sure to understand. I think that if the camera moves, as it become the parent of B, will produce that B would move. Right? I'm taking a look to the function: public void Rotate(Vector3 eulerAngles, Space relativeTo = Space.Self); Maybe using as relativeTo the camera could achieve what your solution tries (or maybe your solution is fully perfect and I'm fully wrong!) – chelder Apr 14 '18 at 21:34
• @chelder even though B is initialized in the camera's position, it does not become it's child. From documentation of Instantiate, if you provide no parent, the object's parent will be the scene itself. This means the object is totally independent from any other object in the scene. – TomTsagk Apr 14 '18 at 21:50
• @chelder I haven't tried the code myself, but it should work. The only mistake I can think of would be if Rotate does not include object's current position, in which case you can make B child of an Empty and attempt something similar – TomTsagk Apr 14 '18 at 21:54