So basically I have an object, a 3D menu, attach itself to a game object when that object is selected. The camera is a first person camera (actually this is for a HoloLens app), so the player can walk around the object and look at it from any angle and I want the 3D menu to move around the object following the player at a particular distance (toward the player) from the root of the object the menu is attached to. The menu is circular so the player can see the menu options around the edge but still see the object as if looking through a scope.
I’m fairly new to Unity and vector math, so I’m having trouble figuring this out the basic calculations that are needed. I am already using a billboard script attached to the circular menu so that it constantly faces the player. I just can’t get it to move in orbit like fashion following the player (Camera.main) position at a set distance from the object.
Note: The menu will appear when the player clicks on the object, but so that the user doesn’t forget to close the menu, it will disappear if the player moves away from the object.
Sorry I don’t really have any code. Basically I just have the game object called ObjectOfMenu.
(This is a first every post/request for help. I defiantly need to learn quite a bit about vector math.)
I’ll asume the object your menu rotates around is a sphere. It might be little bit more complicated to do, if it is a different object, but we could try and tackle that as well.
First things first - we would need two radius’es. One for the sphere object, one for the menu.
Secondly, we need a Vector3 for a direction of where you’d want to put your menu on.
public class Orbit : MonoBehaviour {
// declare both the sphere object and the menu object
public GameObject sphere;
public GameObject menu;
// declare the radius of both objects (basically in this case, the menu would float 0.2 units above the sphere)
public float sphereRadius = 1f;
public float menuRadius = 1.2f;
// the direction that we need
Vector3 dirVector;
void Start(){
// let's make the sphere have the radius that we set
sphere.transform.localScale = Vector3.one * sphereRadius;
}
void Update () {
// to get the direction that the menu has to be on the sphere, we need to
// subtract the sphere objects coordinates, from the camera coordinates
// and afterwards we normalize that vector (so it has magnitude of 1)
dirVector = (Camera.main.transform.position - sphere.transform.position).normalized;
// now we set the position of the menu. From the center of the sphere object, we move it by the radius for the menu we described
// in the direction that we have calculated
menu.transform.position = sphere.transform.position + dirVector * menuRadius;
}
}
I hope this is all clear enough. Do tell if you need something a little bit different.