- Home /
Dragable UI Around the Player Using a Radius
I have a menu based application and I'm wondering how to create a dragable panel(using new UI), that can be dragged off screen but instead of staying on the same axis it rotates around the player. As the main application will be using a head tracking peripheral the player can move his/her head to see the floating panels around them and drag them in this enclosed radius around their head.
I am updating an old application so I've done dragable panels before my question is how to rotate a panel around the camera using a radius, also the UI's local rotation is needs to always be pointing at the player, so the player can see the panels contents when they look at it.
Note my previous build used Mathf.Clamp to clamp the panel within another parent panel
Edit*
I've re-purposed an old camera script for the panels to swivel as the user looks at them it looks quiet nice.
I'm still having issues with maintaining the radius between the panel and the main camera though. My last build has the panels in camera space. The UI panels are no longer subject to a parent panel as that limits the mobility of the panels, and the idea of this application is to have easy to access UIs that rotate around the main camera. There is few problems I have came across.
-The UIs need to be relevant to the main camera as it moves in world space, this is the purpose of camera spce placement.
-The UIs do not need to relevant to the cameras rotation, as the UIs will need to be easily dragged off in any direction and retrieved again by the user looking in that direction and dragging them back in to his/her forward facing direction.
If anyone can please help lead me in the right direction, maybe another similar question, It could even be a basic game object locked to a radius as the new UI system are still Game objects.
Answer by Igneom · Jun 15, 2015 at 09:09 AM
I've had some problems understanding what you want to do exactly, and even though your question is a little bit old, you could set the position of the UI to a position that is greater than the distance you want the UI to be, in the direction of the mouse relative to the camera, then clamp the magnitude of the vector to the distance of the radius around the camera.
using UnityEngine;
using UnityEngine.EventSystems;
public class DraggableAround : MonoBehaviour, IDragHandler
{
public void OnDrag(PointerEventData eventData)
{
// Let's just assume the distance from the camera you want is 5
float distanceFromCamera = 5;
// This is Necessary so we can discover the direction under the mouse from the camera
Ray ray = Camera.mainCamera.ScreenPointToRay(eventData.position);
// This gets a point on the same direction of the ray, but greater than the position of the
// distance from the camera, any number you add works, but adding 1 is enough
Vector3 greaterPosition = ray.GetPoint(distanceFromCamera + 1);
// Here you clamp the magnitude of the greaterPosition according to the distance from the camera
// now assume the camera.position values are x = 0, y = 0, z = 94, the greaterPosition will have
// the values x = 0, y = 0, z = 100, the vector3 returned will have the values x = 0, y = 0, z = 5
// so we add the camera position back so it can be a vector3 relative to the camera
Vector3 radiusPosition = camera.position + Vector3.ClampMagnitude(greaterPosition, distanceFromCamera);
// Just assign the UI object the radiusPosition
transform.position = radiusPosition;
}
}
Completely untested, but should work.
Thank you for your answer, I have been putting off this project for awhile do to other more important matters. I will post on here again and let you know if this works. It does look very promising thank you again. :)
Your answer
Follow this Question
Related Questions
prefabs inside content area not fitting entire width of content area? 0 Answers
[SOLVED] Why is text not appearing? 2 Answers
Please Help Me With My Stamina Bar! 1 Answer
Multiple Cars not working 1 Answer
Problem with jumping in fps game 1 Answer