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

Question Controll Projectile (rigidbody) with Gyro

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

  1. Serge144

    Serge144

    Joined:
    Oct 2, 2018
    Posts:
    64
    Hello everyone.

    So basically I have a Ball, and much like a space ship game, the camera is following this ball right behind it while the ball moves forward in the air, and I need to controll it using the Gyro. I simply wanted to do this: (the game is on landscape mode by the way).

    1. If the person tilts the phone left or right (just like a steering wheel), it should move the ball (rigidbody) also left or right respectively.
    2. If the person tilts the phone forward (i.e in such way that the top edge of the phone goes away from the person) then the Ball should go down, and if it tilts backwards (i.e in such way that the top edge of the phone comes closer to the person) then the Ball should go up.

    How can I make this? Thank you
     
  2. Serge144

    Serge144

    Joined:
    Oct 2, 2018
    Posts:
    64
    I was able to do it doing this:
    Code (CSharp):
    1.    // X axis is the Horizontal axis of the phone in landscape, tilting around this axis means to apply force upwards or downwards.
    2.     rotX += Input.gyro.rotationRateUnbiased.x * 4f;
    3.    
    4.     // Y axis is the Vertical axis of the phone in landscape, tilting around this axis means to apply force left or right.
    5.     rotY += Input.gyro.rotationRateUnbiased.z * 4f;
    6.        
    7.     Rb.AddForce(transform.up * rotX);
    8.     Rb.AddForce(-transform.right * rotY);
    9.        
    10.     if(Input.gyro.rotationRateUnbiased.x == 0){
    11.        rotX = 0;
    12.      }
    13.     if(Input.gyro.rotationRateUnbiased.z == 0){
    14.        rotY = 0;
    15.     }