【发布时间】:2019-04-30 06:16:44
【问题描述】:
目前我正在开发一款射击游戏,它是一款内置 3d 的 2d 游戏。我想使用操纵杆来控制我的玩家平面旋转。我已经在画布上添加了我的操纵杆,并使用 PointersEventData 我正在处理我的操纵杆旋转。 这是它的代码:(ControllerBG 是操纵杆的外圈,控制器是操纵杆的内圈)
public virtual void OnDrag(PointerEventData eventData)
{
Vector2 pos;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(controllerBG.rectTransform, eventData.position, eventData.pressEventCamera, out pos))
{
pos.x = (pos.x / controllerBG.rectTransform.sizeDelta.x);
pos.y = (pos.y / controllerBG.rectTransform.sizeDelta.y);
inputVector = new Vector3(pos.x * 2, 0, pos.y * 2);
inputVector = (inputVector.magnitude > 1.0f) ? inputVector.normalized : inputVector;
//move the joystick inner circle
controller.rectTransform.anchoredPosition =
new Vector3(inputVector.x * (controllerBG.rectTransform.sizeDelta.x / 2),
inputVector.z * (controllerBG.rectTransform.sizeDelta.y / 2));
}
}
现在我想要的是当操纵杆旋转时,我想旋转我的玩家平面,但我不知道如何做到这一点,请帮助我。
到目前为止,我已经尝试过了,但它对我不起作用: 1.
Quaternion targetRotation = Quaternion.LookRotation(controller.rectTransform.anchoredPosition);
plane.transform.rotation = Quaternion.RotateTowards(
plane.transform.rotation,
targetRotation,
PlaneController.instance.steeringPower * Time.deltaTime);
- 还创建了一个函数
private void Rotate(float dir)
{
if (plane != null)
plane.transform.Rotate(Vector3.up * PlaneController.instance.steeringPower * dir * Time.deltaTime * 0.8f);
}
我已经创建了这个 Rotate() 但我不知道如何在操纵杆运动中使用它,即如果控制器(操纵杆)顺时针移动 Rotate(1f) 否则如果逆时针移动 Rotate(-1f)。
请帮我解决我的问题。提前谢谢你。
【问题讨论】:
-
看看这个如果可以帮助。 answers.unity.com/questions/1433049/…。互联网上有大量的链接和教程。你只需要谷歌它。
-
嘿@SaadAnees,实际上我确实在网上寻找了很多教程,但我没有得到我想要的解决方案。你能帮我这个代码吗,我会非常感谢你
-
当然。你能告诉我你想添加哪个操纵杆控件吗?是手机触控摇杆吗?
-
是的,它的移动触摸操纵杆。span>
标签: c# unity3d canvas rotation joystick