Looking for help with orbit/follow player script vector math calculations
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.)
Answer by Scoutas · Jan 11, 2017 at 03:02 AM
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.
Well, just before I checked this post, I managed to figure it out on my own and came up with basically what you provided. Only difference is that no, the object isn't a sphere and rather than use a radius, I am just using an offset float. Although I may look into using an invisible sphere if it's not typical for the root to be at the exact center of the object I'm showing the menu for. I'll have to play with that.
void Update()
{
Vector3 direction = (Camera.main.transform.position - ObjectOf$$anonymous$$enu.transform.position).normalized;
this.transform.position = ObjectOf$$anonymous$$enu.transform.position + direction * Offset;
}
Anyhow, I really appreciate your response and am amazed at how much I have to learn about this stuff! But I'm glad I didn't give up, pleased with the results, and very very thankful for your help kind person!
Your answer
Follow this Question
Related Questions
How to calculate forward/Up/Right without transform, have only vector3? 0 Answers
Rotate around an object to its direction of velocity (C#) 0 Answers
ClampMagnitude while ignoring a direction 0 Answers
How to make a object move forward when is rotated. 0 Answers
change angle towards direction 1 Answer