手机上的Unity鼠标输入

时间:2020-06-14 11:09:38

标签: c# android unity3d input

我是Unity的新手,并且正在制作第一人称游戏。我观看了这段视频https://youtu.be/_QajrabyTJc,并在尝试使用Unity遥控器5时,相机没有旋转。代码在这里:

     public float mouseSensitivity = 100f;

    public Transform playerBody;

    float xRotation = 0f;


    void Update()
    {
        float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
        float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;



        xRotation -= mouseY;
        xRotation = Mathf.Clamp(xRotation, -90f, 90f);

        transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
        playerBody.Rotate(Vector3.up * mouseX);

    }

如何解决此问题?还是仅因为我使用的是Unity Remote 5,而不是因为我在Android移动设备上使用?

1 个答案:

答案 0 :(得分:2)

对于在移动设备上进行触摸输入,您宁愿使用const maskAccountId = (accountId) => { if (accountId) { /** Condition will only executes if accountId is *not* undefined, null, empty, false or 0*/ const accountIdlength = accountId.length; const maskedLength = accountIdlength - 4; /** Modify the length as per your wish */ let newString = accountId; for (let i = 0; i < accountIdlength; i++) { if (i < maskedLength) { newString = newString.replace(accountId[i], '*'); } } return newString; } else return /**Will handle if no string is passed */ } console.log(maskAccountId('egrgrgry565yffvfdfdfdfdfgrtrt4t4'));并执行类似的操作

Input.touches

请注意,// I would use a Vector2 here in order to be able // to have a different sensitivity for the two axis public Vector2 mouseSensitivity = Vector2.one * 100f; public Transform playerBody; private Vector2 startPos; private float startRot; private Quaternion originalBodyRot; void Update() { if(Input.touchCount == 1) { var touch = Input.GetTouch(0); switch(touch.phase) { case TouchPhase.Began: startPos = touch.position; startRot = transform.localEulerAngles.x; originalBodyRot = playerBody.rotation; break; case TouchPhase.Moved: var delta = Vector2.Scale(touch.position - startPos, mouseSensitivity); var newRot = Mathf.Clamp(startRot - delta.y, -90, 90); transform.localEulerAngles = new Vector3 (newRot, 0, 0); playerBody.rotation = originalBodyRot * Quaternion.Euler(0, delta.x, 0); break; } } } 位于像素空间中,因此您可能需要调整灵敏度。


注意:在智能手机上输入文字,但我希望这个想法会清楚