在编辑器中旋转平滑但在设备中不旋转

我有一个 3D 对象,我想用鼠标/手指滑动来旋转它,所以我制作了下面的脚本。


对象的旋转在编辑器上看起来很流畅,但是在真实设备(android)上玩游戏时,旋转并没有立即跟随手指移动,跟随手指需要几毫秒,它不流畅并且控制变得困难并且令人沮丧!


float sensitivity = 0.8f;

Vector2 firstPressPos;

Vector2 secondPressPos;


void Update()

{


    if (Input.GetMouseButtonDown(0))

    {

        //save began touch 2d point

        firstPressPos = new Vector3(Input.mousePosition.x, Input.mousePosition.y);

    }


    if (Input.GetMouseButton(0))

    {

        //save ended touch 2d point

        secondPressPos = new Vector3(Input.mousePosition.x, Input.mousePosition.y);


        if (firstPressPos != secondPressPos)

        {

            float RotX = Input.GetAxis("Mouse X") * sensitivity * Time.deltaTime;

            float RotY = Input.GetAxis("Mouse Y") * sensitivity * Time.deltaTime;

            transform.RotateAround(Vector3.up, RotX);

            transform.RotateAround(Vector3.right, -RotY);

        }

    }

}


三国纷争
浏览 75回答 2
2回答

HUH函数

试试这个代码// Screen Touches    Vector2?[] oldTouchPositions = {        null,        null    };    // Rotation Speed    public float rotSpeed = 0.5f;    public void Update()    {        if (Input.touchCount == 0)        {            oldTouchPositions[0] = null;            oldTouchPositions[1] = null;        }        else if (Input.touchCount == 1)        {            if (oldTouchPositions[0] == null || oldTouchPositions[1] != null)            {                oldTouchPositions[0] = Input.GetTouch(0).position;                oldTouchPositions[1] = null;            }            else            {                Vector2 newTouchPosition = Input.GetTouch(0).position;                float distanceX = (oldTouchPositions[0] - newTouchPosition).Value.x;                float distanceY = (oldTouchPositions[0] - newTouchPosition).Value.y;                float rotX = distanceX * rotSpeed * Mathf.Deg2Rad;                float rotY = distanceY * rotSpeed * Mathf.Deg2Rad;                transform.Rotate(Vector3.up, rotX * 5, Space.Self);                transform.Rotate(Vector3.left, rotY * 5, Space.Self);                oldTouchPositions[0] = newTouchPosition;            }        }        else        {            if (oldTouchPositions[1] == null)            {                oldTouchPositions[0] = Input.GetTouch(0).position;                oldTouchPositions[1] = Input.GetTouch(1).position;            }            else            {            }        }    }

慕村9548890

这是我几年前发现的一个脚本,它使用鼠标或触摸操作 X 轴旋转。它似乎适用于触摸,但不适用于鼠标。尝试一下,让我知道它是否表现更好。这个应该动态调整旋转速度:using System.Collections;using System.Collections.Generic;using UnityEngine;public class SpinDrag : MonoBehaviour {&nbsp; &nbsp; float f_lastX = 0.0f;&nbsp; &nbsp; float f_difX = 0.5f;&nbsp; &nbsp; float f_steps = 0.0f;&nbsp; &nbsp; int i_direction = 1;&nbsp; &nbsp; // Use this for initialization&nbsp; &nbsp; void Start()&nbsp; &nbsp; {&nbsp; &nbsp; }&nbsp; &nbsp; // Update is called once per frame&nbsp; &nbsp; void Update()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (Input.GetMouseButtonDown(0))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; f_difX = 0.0f;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else if (Input.GetMouseButton(0))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; f_difX = Mathf.Abs(f_lastX - Input.GetAxis("Mouse X"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (f_lastX < Input.GetAxis("Mouse X"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i_direction = -1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; transform.Rotate(Vector3.up, -f_difX);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (f_lastX > Input.GetAxis("Mouse X"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i_direction = 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; transform.Rotate(Vector3.up, f_difX);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; f_lastX = -Input.GetAxis("Mouse X");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (f_difX > 0.5f) f_difX -= 0.05f;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (f_difX < 0.5f) f_difX += 0.05f;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; transform.Rotate(Vector3.up, f_difX * i_direction);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP