简体   繁体   English

对象更改旋转跳转到它原来的 Unity3D

[英]Objects changed rotation jumps to it's original Unity3D

I made input joysticks for mobile game.我为手机游戏制作了输入操纵杆。 One that moves the player and other one that changes it's rotation.一个移动玩家,另一个改变它的旋转。 Both are working fine but only problem is that when I change players rotation and release that joystick it jumps to it's original rotation.两者都工作正常,但唯一的问题是,当我改变玩家旋转并释放操纵杆时,它会跳到原来的旋转。 Here's my script:这是我的脚本:

public GameObject player;
private PlayerInput playerInput;

void Update()
{
    Vector2 input = playerInput.actions["Move"].ReadValue<Vector2>();
    Vector3 move = new Vector3(input.x, input.y, 0);
    player.transform.position += move * speed * Time.deltaTime;

    Vector2 look = playerInput.actions["Look"].ReadValue<Vector2>();
    player.transform.rotation = Quaternion.LookRotation(Vector3.forward, look);
}

I typically solve this by ignoring rotation values of small magnitude and maintaining the last frame's orientation in said case.我通常通过忽略小幅度的旋转值并在上述情况下保持最后一帧的方向来解决这个问题。 You might have to tune the threshold below to filter out the correct values.您可能需要调整下面的阈值以过滤掉正确的值。

public GameObject player;
private PlayerInput playerInput;
private Vector2 look;

void Update()
{
    Vector2 input = playerInput.actions["Move"].ReadValue<Vector2>();
    Vector3 move = new Vector3(input.x, input.y, 0);
    player.transform.position += move * speed * Time.deltaTime;

    var inputLook = playerInput.actions["Look"].ReadValue<Vector2>();
    look = inputLook.magnitude > 0.1f ? inputLook : look;
    player.transform.rotation = Quaternion.LookRotation(Vector3.forward, look);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM