Search Unity

Ball movement with gyroscope

Discussion in 'Physics' started by bartekpacia, Aug 20, 2017.

  1. bartekpacia

    bartekpacia

    Joined:
    Aug 20, 2017
    Posts:
    3
    Hi all, it's my first post in Unity community forum:)

    I'm currently trying to make my ball moving by using gyroscope. I want to use almost the same movement logic as i used in PC movement. On PC it works well.However, on mobiles i can move my ball only up and down. I have no idea what is wrong in my code, please, help me.

    That's a piece of PlayerController script, that matters:
    Code (CSharp):
    1. void Update ()
    2.     {
    3.         Rigidbody rigidbody = transform.GetComponent<Rigidbody>();
    4.         Vector3 direction = Vector3.zero;
    5.  
    6.         //PC code works great
    7.         if (SystemInfo.deviceType == DeviceType.Desktop)
    8.         {
    9.             if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W))
    10.             {
    11.                 direction = -Vector3.left;
    12.             }
    13.  
    14.             if (Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S))
    15.             {
    16.                 direction = Vector3.left;
    17.             }
    18.  
    19.             if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
    20.             {
    21.                 direction = Vector3.forward;
    22.             }
    23.  
    24.             if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
    25.             {
    26.                 direction = -Vector3.forward;
    27.             }
    28.  
    29.             rigidbody.AddTorque(direction * speed);
    30.  
    31.         }
    32.  
    33.         //My ball moves only up and down, no left or right
    34.         if (SystemInfo.deviceType == DeviceType.Handheld)
    35.         {
    36.            
    37.             float x = Input.acceleration.x;
    38.             float y = Input.acceleration.y;
    39.  
    40.             if (x > 0)
    41.             {
    42.                 direction = -Vector3.forward;
    43.             }
    44.  
    45.             if (x < 0)
    46.             {
    47.                 direction = Vector3.forward;
    48.             }
    49.  
    50.             if (y > 0)
    51.             {
    52.                 direction = -Vector3.left;
    53.             }
    54.  
    55.             if (y < 0)
    56.             {
    57.                 direction = Vector3.left;
    58.             }        
    59.  
    60.             rigidbody.AddTorque(direction * speed);                                
    61.         }
    62.     }
     
  2. Johannski

    Johannski

    Joined:
    Jan 25, 2014
    Posts:
    826
    Well, your Input.acceleration will probably always be a non zero value, but your logic overrides your input, because you are setting an absolute value for your direction.
    Set a threshold and allow vertical and horizontal input at teh same time:
    Code (CSharp):
    1. //My ball moves only up and down, no left or right
    2. if (SystemInfo.deviceType == DeviceType.Handheld)
    3. {
    4.     float threshold = 0.2f;
    5.     float x = Input.acceleration.x;
    6.     float y = Input.acceleration.y;
    7.    
    8.     if (x > threshold)
    9.     {
    10.         direction -= Vector3.forward;
    11.     }
    12.  
    13.     if (x < -threshold)
    14.     {
    15.         direction += Vector3.forward;
    16.     }
    17.  
    18.     if (y > threshold)
    19.     {
    20.         direction -= Vector3.left;
    21.     }
    22.  
    23.     if (y < -threshold)
    24.     {
    25.         direction += Vector3.left;
    26.     }      
    27.     direction = direction.normalized;
    28.    
    29.     rigidbody.AddTorque(direction * speed);                              
    30. }
     
  3. bartekpacia

    bartekpacia

    Joined:
    Aug 20, 2017
    Posts:
    3
    Thank You for reply.

    I copied your code, but now my ball doesn's move anywhere. Just stays on spawn :(
     
  4. bartekpacia

    bartekpacia

    Joined:
    Aug 20, 2017
    Posts:
    3
    update

    Please, help me, I'm sure somebody knows the solution :(
     
  5. BoogieD

    BoogieD

    Joined:
    Jun 8, 2016
    Posts:
    236
    Johannski is correct about what he said. Your y input code is overwriting your x input code. That is why it was only going up and down. Just Debug.Log or display 'direction' values so you can analyse what is going on. Also, you should really be using an 'else' prior to every second 'if'. Learn to debug otherwise it will always be WTF?
     
    Last edited: Sep 3, 2017