将Android操纵杆和触摸控件添加到Unity上的3D角色

问题描述 投票:0回答:2

有人可以告诉我如何使用移动操纵杆移动3D角色吗?我正在Unity上为Android开发,而我坚持这一部分,基本上,我希望能够用操纵杆移动角色,然后能够通过触摸屏幕进行旋转。

android unity3d
2个回答
0
投票

对于插入的操纵杆,基本上就像任何PC操纵杆一样。因此,您只需要使用老式的Input.GetAxis("Vertical")Input.GetAxis("Horizontal")方法。确保将那些映射到项目首选项>输入]中的来自所有操纵杆的输入中。Official doc

关于触摸,它也很容易与Input.GetTouch(0).position

一起使用。Official doc

您可能还希望对触摸式水龙头进行光线投射,以获取所触摸位置的世界范围位置:

private static Vector3 GetPositionFromScreenPosition(Vector3 screenPosition)
{
    Ray ray = Camera.main.ScreenPointToRay(screenPosition);

    if (Physics.Raycast(ray, out RaycastHit hit))
    {
        return hit.point;
    }

    return Vector3.zero;
}

两者都可以在相同的[[Update()

FixedUpdate()方法上很好地工作。

0
投票
这是我当前的代码:
© www.soinside.com 2019 - 2024. All rights reserved.