【问题标题】:Unity - make character walk towards touch/drag position? In 3D space?Unity - 让角色走向触摸/拖动位置?在 3D 空间中?
【发布时间】:2019-03-03 17:18:05
【问题描述】:

好的,我有一个为 iOS 构建的 3D Unity 游戏,其中将实现这些控件(以第一人称射击游戏的方式):

  1. 镜头紧跟在角色身后
  2. 手指在屏幕上使角色行走,手指向上使其停止
  3. 这就是问题所在:无论用户从哪里开始触摸屏幕,无论他们拖动的方向是什么(向前、向后、左右),角色都会无限期地朝那个方向行走。

这一定已经完成了,但我在任何地方都找不到任何东西。我将相机作为角色的孩子,所以已经处理好了,但对于 char 运动,我所拥有的只是:

 void Update()
    {
        if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved)
        {

            Vector3 target = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, -10f));
            transform.Translate(Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime) - transform.position);

        } 

这在需要时不起作用。我可以在这里做什么?

【问题讨论】:

    标签: c# unity3d physics game-development


    【解决方案1】:

    例如,您可以尝试使用任何 UI 操纵杆之王 (https://assetstore.unity.com/packages/tools/input-management/joystick-pack-107631)

    并像这样访问它的参数:

    public class TestMovement : MonoBehaviour
    {
        public float Speed;
        public FloatingJoystick Joystick; //Set in the inspector, prefab from the asset
        private void Update()
        {
            transform.Translate(new Vector2(Joystick.Horizontal, Joystick.Vertical) * Speed * Time.deltaTime);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多