Search Unity

My accelerometer input works fine through Unity remote, but doesn't work when I build on ios

Discussion in 'iOS and tvOS' started by shabbacreates, Mar 16, 2019.

  1. shabbacreates

    shabbacreates

    Joined:
    Jan 9, 2019
    Posts:
    7
    Hey guys,

    My accelerometer input works fine when I run my game through Unity remote, but doesn't work when I build it through XCode and run it on my iPhone that way. I have already checked when I built my game my accelerometer frequency was set to 60Hz but it still doesn't work.

    Here is my code, any help is highly appreciated!
    Code (CSharp):
    1.     void AcceleratorMove()
    2.     {
    3.         float x = Input.acceleration.x;
    4.         if (x<-0.1f)
    5.         {
    6.             MoveLeft();
    7.         }
    8.         else if (x > 0.1f)
    9.         {
    10.             MoveRight();
    11.         }
    12.         else
    13.         {
    14.             SetVelocityZero();
    15.         }
    16.  
    17.     }
    18.      void MoveLeft()
    19.     {
    20.         position.x += -1 * Time.deltaTime * horizontalPositionSpeedMultiplier;
    21.     }
    22.     void MoveRight()
    23.     {
    24.         position.x += 1 * Time.deltaTime * horizontalPositionSpeedMultiplier;
    25.     }
    26.     void SetVelocityZero()
    27.     {
    28.         position.x += 0 * Time.deltaTime * horizontalPositionSpeedMultiplier;
    29.     }
     
  2. Johannski

    Johannski

    Joined:
    Jan 25, 2014
    Posts:
    826
    There is no problem in your code. If you set the accelerometer frequency in the build settings correctly (which you said you did), I guess there might be a #if UNITY_EDITOR code snippet or an EditorOnly tag on a game object so the script won't be executed in a build. Maybe post thhe complete script so we can dissect it further.
     
  3. shabbacreates

    shabbacreates

    Joined:
    Jan 9, 2019
    Posts:
    7
    Ok thanks. So this is my full code. Please let me know what you think?
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class carController : MonoBehaviour
    7. {
    8.     protected float carSpeed = 2f;
    9.     protected float maxPos = 2.4f;
    10.     public const float horizontalPositionSpeedMultiplier = 2.5f;
    11.     public GameObject crashsplosion;
    12.  
    13.  
    14.     protected Vector2 position;
    15.     public UIManager uimanager;
    16.     public Vector3 respawnPosition;
    17.  
    18.  
    19.  
    20.     protected levelManager LevelManager;
    21.     protected SoundEffect soundEffect;
    22.  
    23.  
    24.     private  protected bool RightTurn;
    25.     private  protected bool LeftTurn;
    26.     private protected bool StraightMove = true;
    27.     private protected Animator myAnim;
    28.  
    29.     private protected float[] PositionsInTrack = new float[] { -2.12f, -0.72f, 0.68f, 2.08f };
    30.  
    31.  
    32.     // Start is called before the first frame update
    33.  
    34.     protected void Start()
    35.     {
    36.  
    37.         LevelManager = FindObjectOfType<levelManager>();
    38.         soundEffect = FindObjectOfType<SoundEffect>();
    39.         position = transform.position;
    40.         myAnim = GetComponent<Animator>();
    41.         position = transform.position;
    42.  
    43.      
    44.  
    45.     }
    46.  
    47.     // Update is called once per frame
    48.     protected void Update()
    49.     {
    50. #if UNITY_IOS
    51.         {
    52.        
    53.         AcceleratorMove();
    54.  
    55.         }
    56.  
    57.  
    58. #endif
    59.  
    60. #if  UNITY_STANDALONE
    61.  
    62.  
    63.         {
    64.             position.x += Input.GetAxis("Horizontal") * Time.deltaTime * horizontalPositionSpeedMultiplier;
    65.             position.x = Mathf.Clamp(position.x, -maxPos, maxPos);
    66.             transform.position = position;
    67.  
    68.             if (Input.GetAxisRaw ("Horizontal") > 0.1f)
    69.             {
    70.                 RightTurn = true;
    71.                 StraightMove = false;
    72.             }
    73.             else if (Input.GetAxisRaw("Horizontal") < -0.1f)
    74.             {
    75.                 LeftTurn = true;
    76.                 StraightMove = false;
    77.             }
    78.  
    79.         }
    80. #endif
    81. #if UNITY_ANDROID
    82.         {
    83.             AcceleratorMove();
    84.         }
    85. #endif
    86.  
    87.  
    88.         position = transform.position;
    89.         position.x = Mathf.Clamp(position.x, -maxPos, maxPos);
    90.         transform.position = position;
    91.  
    92.         if (Input.acceleration.x  > 0.1f)
    93.         {
    94.             RightTurn = true;
    95.             StraightMove = false;
    96.         }
    97.         else if(Input.acceleration.x < -0.1f)
    98.         {
    99.             LeftTurn = true;
    100.             StraightMove = false;
    101.         }
    102.         myAnim.SetBool("RightTurn", RightTurn);
    103.         myAnim.SetBool("LeftTurn", LeftTurn);
    104.         myAnim.SetBool("StraightMovement", StraightMove);
    105.  
    106.         RightTurn = false;
    107.         StraightMove = true;
    108.         LeftTurn = false;
    109.     }
    110.  
    111.  
    112.     public void Crashexplosion()
    113.     {
    114.         Instantiate(crashsplosion, transform.position, transform.rotation);
    115.     }
    116.    protected void AcceleratorMove()
    117.     {
    118.         float x = Input.acceleration.x;
    119.         if (x<-0.1f)
    120.         {
    121.             MoveLeft();
    122.         }
    123.         else if (x > 0.1f)
    124.         {
    125.             MoveRight();
    126.         }
    127.         else
    128.         {
    129.             SetVelocityZero();
    130.         }
    131.  
    132.     }
    133.     public void MoveLeft()
    134.     {
    135.         position.x += -1 * Time.deltaTime * horizontalPositionSpeedMultiplier;
    136.     }
    137.    public void MoveRight()
    138.     {
    139.         position.x += 1 * Time.deltaTime * horizontalPositionSpeedMultiplier;
    140.     }
    141.    public void SetVelocityZero()
    142.     {
    143.         position.x += 0 * Time.deltaTime * horizontalPositionSpeedMultiplier;
    144.     }
    145.    protected virtual void OnTriggerEnter2D(Collider2D col)
    146.     {
    147.         if(col.gameObject.tag == "EnemyCar" || col.gameObject.tag == "Okada" || col.gameObject.tag == "Keke" || col.gameObject.tag == "BRT")
    148.         {
    149.             Destroy(col.gameObject);
    150.             respawnPosition = position;
    151.             soundEffect.SingleDamageSoundEffect();
    152.         }
    153.         else if (  col.gameObject.tag == "Police")
    154.         {
    155.             Destroy(col.gameObject);
    156.             respawnPosition = position;
    157.             soundEffect.PoliceSoundEffect();
    158.         }
    159.         else if ( col.gameObject.tag == "Trailer")
    160.         {
    161.             Destroy(col.gameObject);
    162.             respawnPosition = position;
    163.             soundEffect.TrailerSoundEffect();
    164.  
    165.         }
    166.         else if (col.gameObject.tag == "Pothole")
    167.         {
    168.             Destroy(col.gameObject);
    169.             float xPos = PositionsInTrack[Random.Range(0, 4)];
    170.             respawnPosition = new Vector2(xPos,position.y);
    171.             position = respawnPosition;
    172.             soundEffect.PotholeEffect();
    173.         }
    174.  
    175.         LevelManager.Respawn();
    176.      
    177.  
    178.     }
    179.  
    180.  
    181. }
    182.  
     
  4. Johannski

    Johannski

    Joined:
    Jan 25, 2014
    Posts:
    826
    The error is at line 88. You're overwriting your just calculated position with transform.position. This is no problem for standalone since there you're also setting the transform.position value. I suggest moving line 88 to the start of the update method.
     
    shabbacreates likes this.
  5. shabbacreates

    shabbacreates

    Joined:
    Jan 9, 2019
    Posts:
    7
    Thank you, you are an absolute genius!!! The problem is solved. thank you