简体   繁体   中英

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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