Reputation: 26
I want a curved platform to face towards the cursor and rotate around a red ball in the center (0f, 0f), while keeping a distance of 0.6f from the ball. Essentially the platform would want to keep as short distance from the cursor as possible, without escaping the red balls gravity.
(Picture of the platform and the red ball)
I tried using ScreenToWorldPoint in transform.LookAt to make the platform look at the cursor, however this will rotate the platform around its own axis, not the balls. RotateAround didnt work for me neither, as i want to be able to rotate the platform with my mouse.
Im wondering if i could set a custom axis for the platform and then rotate it towards the cursor?
Upvotes: 1
Views: 1010
Reputation: 20249
With the local direction of the platform the you want to point at the cursor as Vector2 localForwardDirection
, the distance you want from the surface of the ball float distFromBallSurface
, and the radius of the ball float ballRadius
:
Rotate the platform (you can use Quaternion.FromToRotation
to rotate the "front" to be from whatever direction to point towards the cursor), then place it accordingly:
Vector2 ballToCursor = cursorPositionWorldSpace - ballObject.position;
platformObject.rotation = Quaternion.fromToRotation(localForwardDirection,ballToCursor);
platformObject.position = ballObject.position
+ ballToCursor.normalized * (ballRadius + distFromBallSurface);
Upvotes: 2