Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Set animation based on Gyro rotation.

Discussion in 'Input System' started by Serge144, May 14, 2022.

  1. Serge144

    Serge144

    Joined:
    Oct 2, 2018
    Posts:
    64
    The character in my game doesn't have free movement, it simply rotates around a central point while the camera is right behind the character.

    This is a Top View example:
    upload_2022-5-14_8-41-34.png


    So If I simply Aim my phone to the left with gyro, the character (In Blue) will rotate to the right. If I rotate my phone to the right the character will rotate to the left.

    This I am already doing with Gyro with this code:
    Code (CSharp):
    1. void GyroControll()
    2. {
    3.    float x = Input.gyro.rotationRateUnbiased.x;
    4.    float y = Input.gyro.rotationRateUnbiased.y;
    5.  
    6.    transform.RotateAround(transform.position, transform.right, -x * GyroSensitivity * Time.deltaTime);
    7.    transform.RotateAround(transform.position, Vector3.up, -y * GyroSensitivity * Time.deltaTime);
    8. }

    What Im trying to do now, is to simply call an Animation for the Character based on its movement (either the character is WalkingLeft or WalkingRight). To do that I added the following block of code to the end of the method above:

    Code (CSharp):
    1. // the problem is that X fluctuates really fast
    2. PlayerAnimator.SetFloat("Speed", Mathf.Abs(x));
    3. PlayerAnimator.SetBool("WalkingLeft", x > 0.15f); //used to be 0
    4. PlayerAnimator.SetBool("WalkingRight", x < -0.15f); //used to be 0
    5.  
    The problem is that X fluctuates so fast between negative and positive values (even when rotating only left for example) That it will simply transition between the animations and I dont get a clear animation.

    I tried to activate the animation only after a certain threshold (0.15f or -0.15f) but with not much effect.
    How could I improve this? o_O
     
    tantx likes this.
  2. Serge144

    Serge144

    Joined:
    Oct 2, 2018
    Posts:
    64
    Any hints on this guys?