如何根据操纵杆旋转来旋转我的飞机?
How can I rotate my plane as per joystick rotation?
目前我正在开发一款射击游戏,它是一款内置 3d 的 2d 游戏。我想用操纵杆来控制我的玩家平面旋转。我在 canvas 上添加了操纵杆,并使用 PointersEventData 处理操纵杆旋转。
这是它的代码:(ControllerBG 是 Joystick 的 outerCircle & Controller 是 Joystick 的 innerCircle)
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)。
请帮我解决我的问题。提前谢谢你。
所以这是你的 OnDrag
函数:
public virtual void OnDrag(PointerEventData eventData)
{
Vector2 pos = Vector2.zero;
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);
float x = (controllerBG.rectTransform.pivot.x == 1) ? pos.x * 2 + 1 : pos.x * 2 - 1;
float y = (controllerBG.rectTransform.pivot.x == 1) ? pos.y * 2 + 1 : pos.y * 2 - 1;
inputVector = new Vector3(x, 0, y);
inputVector = (inputVector.magnitude > 1.0f) ? inputVector.normalized : inputVector;
controller.rectTransform.anchoredPosition =
new Vector3(inputVector.x * (controllerBG.rectTransform.sizeDelta.x / 2),
inputVector.z * (controllerBG.rectTransform.sizeDelta.y / 2));
}
}
我假设您已经实现了 OnPointerDown
和 OnPointerUp
来处理与游戏杆相关的其他事件。我还假设 inputVector
是一个 public 变量以在外部使用它,或者您可以在同一脚本中使用它。这真的取决于你。
public Transform rot;
void Update()
{
Vector3 direction = new Vector3();
direction = inputVector;
rot.Rotate(direction);
}
希望能帮到您解决问题。
目前我正在开发一款射击游戏,它是一款内置 3d 的 2d 游戏。我想用操纵杆来控制我的玩家平面旋转。我在 canvas 上添加了操纵杆,并使用 PointersEventData 处理操纵杆旋转。 这是它的代码:(ControllerBG 是 Joystick 的 outerCircle & Controller 是 Joystick 的 innerCircle)
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)。
请帮我解决我的问题。提前谢谢你。
所以这是你的 OnDrag
函数:
public virtual void OnDrag(PointerEventData eventData)
{
Vector2 pos = Vector2.zero;
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);
float x = (controllerBG.rectTransform.pivot.x == 1) ? pos.x * 2 + 1 : pos.x * 2 - 1;
float y = (controllerBG.rectTransform.pivot.x == 1) ? pos.y * 2 + 1 : pos.y * 2 - 1;
inputVector = new Vector3(x, 0, y);
inputVector = (inputVector.magnitude > 1.0f) ? inputVector.normalized : inputVector;
controller.rectTransform.anchoredPosition =
new Vector3(inputVector.x * (controllerBG.rectTransform.sizeDelta.x / 2),
inputVector.z * (controllerBG.rectTransform.sizeDelta.y / 2));
}
}
我假设您已经实现了 OnPointerDown
和 OnPointerUp
来处理与游戏杆相关的其他事件。我还假设 inputVector
是一个 public 变量以在外部使用它,或者您可以在同一脚本中使用它。这真的取决于你。
public Transform rot;
void Update()
{
Vector3 direction = new Vector3();
direction = inputVector;
rot.Rotate(direction);
}
希望能帮到您解决问题。