简体   繁体   English

Unity-在查看鼠标指针时围绕枢轴旋转

[英]Unity - Rotate around pivot while looking at mouse pointer

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. 我希望弯曲的平台面向光标并围绕中心的红色球(0f,0f)旋转,同时保持与球的距离为0.6f。 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. 我尝试在transform.LookAt中使用ScreenToWorldPoint使平台看着光标,但是这将使平台绕其自身的轴而不是球旋转。 RotateAround didnt work for me neither, as i want to be able to rotate the platform with my mouse. RotateAround也不适合我,因为我希望能够用鼠标旋转平台。

Im wondering if i could set a custom axis for the platform and then rotate it towards the cursor? 我想知道是否可以为平台设置自定义轴,然后将其向光标旋转?

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 : 使用平台的本地方向,您要指向光标的对象是Vector2 localForwardDirection ,您想要的到球float distFromBallSurface表面的距离float distFromBallSurface以及球float distFromBallSurface的半径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: 旋转平台(您可以使用Quaternion.FromToRotation将“ front”旋转为从任意方向指向光标),然后相应地放置它:

Vector2 ballToCursor = cursorPositionWorldSpace - ballObject.position;
platformObject.rotation = Quaternion.fromToRotation(localForwardDirection,ballToCursor);

platformObject.position = ballObject.position 
                          + ballToCursor.normalized * (ballRadius + distFromBallSurface); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM