Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

Android Game Issue (Fast Bug)

Discussion in 'Editor & General Support' started by iamfeyre, Oct 5, 2018.

  1. iamfeyre

    iamfeyre

    Joined:
    May 20, 2016
    Posts:
    49
    okay so i made a game... https://play.google.com/store/apps/details?id=com.ATaleInWinter.CosmicRush

    the problem is..
    on majority of phones it works perfectly..

    however on some phones (like motorolla in this case), it speeds up randomly at places and plays wierdly.. why is this happening?

    I'm 100% sure the code is perfect...
    How can i fix this?

    Thank You So Much!!
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    5,227
    This is why you're unable to find the issue ^^^
     
    xVergilx and JeffDUnity3D like this.
  3. iamfeyre

    iamfeyre

    Joined:
    May 20, 2016
    Posts:
    49
    I'm not kidding... It works perfectly on windows platform and on some mobiles like oneplus and samsung... Only some phones like motorolla have the issues... how is that possible??
     
  4. iamfeyre

    iamfeyre

    Joined:
    May 20, 2016
    Posts:
    49
    please help me out
     
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    5,227
    You haven't given hardly any information about the specific problem. When a game appears to speed up and slow down it often is caused by the pace of the game being tied to the frame rate in your code, so on high performing hardware you don't notice it because it is capable of a stable frame rate, but on lower performing hardware when the frame rate drops you see the pace of the game drop with the frame rate.

    Often this is resolved by multiplying anything you do by Time.deltaTime. If that isn't your issue, you'll need to explain the problem better and probably provide some code related to the problem area(s).
     
    xVergilx likes this.
  6. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    4,138
    As was stated, "I'm 100% sure the code is perfect" is likely covering the real issue. Apparently your code is NOT perfect, and not correctly handling this (possible edge) case. Please provide the code that you believe is perfect, but not functioning correctly. You can recreate it, right? If you can recreate it, then you can (likely!) fix it.
     
    Joe-Censored likes this.
  7. iamfeyre

    iamfeyre

    Joined:
    May 20, 2016
    Posts:
    49
    okay so i can't post the code here but I'll tell you how it was.

    in Update -
    first all inputs were taken and i set the rotation of player based on inputs
    if jump button pressed a Force.IMPULSE was given to player

    in FixedUpdate -
    a constant forward velocity * time.deltaTime was given to player if input is not 0.

    Every obstacle just has Transform.Translate with speed multiplied by time.deltaTime in Update().

    I tried putting everything in Update() and everything in FixedUpdate() too.. didn't work.

    PS - i have noticed that the game loses control when approaching certain obstacles...

    Please help..
     
  8. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    4,138
    Without seeing the actual code, unfortunately I would not be able to help.
     
  9. iamfeyre

    iamfeyre

    Joined:
    May 20, 2016
    Posts:
    49
    I'm really sorry for replying late my laptop stopped working...

    okay so here is the code.. it randomly speeds up the player too much and again randomly goes back to normal...

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerActionsAndroid : MonoBehaviour {
    6.    
    7.     Rigidbody playerRigidbody;
    8.     Animator playerAnimator;
    9.     [SerializeField] float speed;
    10.     Vector3 prevEulers, curForward;
    11.  
    12.     [SerializeField] Transform groundCheckPos;
    13.     [SerializeField] float gcBoxLength, gcBoxWidth, gcBoxHeight;
    14.     [SerializeField] LayerMask whatIsGround;
    15.     bool isGrounded;
    16.  
    17.     [Space(10)]
    18.     [SerializeField] float jumpSpeed;
    19.     bool canDoubleJump;
    20.     bool doJump, doDoubleJump;
    21.  
    22.     [Space(10)]
    23.     public static bool gameFinished;
    24.  
    25.     [Space(10)]
    26.     [SerializeField] AudioClip jumpAudio, doubleJumpAudio;
    27.     [SerializeField] AudioSource audioPlayer;
    28.  
    29.     [Space(10)]
    30.     [SerializeField] float mobileJoystickDeadzone;
    31.     bool mobileMoveOriginSet;
    32.     Vector2 moveOrigin;
    33.     int moveTouch, moveFingerID;
    34.     float jumpStart;
    35.     [SerializeField] float timeBetweenJump;
    36.     [SerializeField] GameObject touchJoystick;
    37.     GameObject currJoystick;
    38.  
    39.     void Start()
    40.     {
    41.         playerRigidbody = transform.GetComponent<Rigidbody>();
    42.         playerAnimator = transform.GetComponent<Animator>();
    43.  
    44.         prevEulers = Vector3.up * transform.rotation.eulerAngles.y;
    45.         isGrounded = false;
    46.         canDoubleJump = true;
    47.         doJump = false;
    48.         doDoubleJump = false;
    49.  
    50.         gameFinished = false;
    51.  
    52.         mobileMoveOriginSet = false;
    53.         currJoystick = null;
    54.     }
    55.  
    56.     void Update()
    57.     {
    58.  
    59.         // Grounded Check -------
    60.  
    61.         isGrounded = (Physics.OverlapBox(groundCheckPos.position, new Vector3(gcBoxWidth, gcBoxHeight, gcBoxLength), Quaternion.identity, whatIsGround).Length) > 0;
    62.         if (isGrounded) canDoubleJump = true;
    63.  
    64.         // Grounded Check End -------
    65.  
    66.         // Movement -------
    67.  
    68.         // Get axes input
    69.         float Horizontal = Input.GetAxis("Horizontal");
    70.         float Vertical = Input.GetAxis("Vertical");
    71.  
    72.         bool isRunning = false;
    73.        
    74.         // Mobile Input --------------
    75.  
    76.         float half = Screen.width / 2f;
    77.         Touch[] touches = Input.touches;
    78.         bool mobileJump = false;
    79.  
    80.         if (touches.Length > 0 && Time.timeScale != 0)
    81.         {
    82.  
    83.             for (int i = 0; i < Input.touchCount; i++)
    84.             {
    85.                 if (mobileMoveOriginSet && touches[i].fingerId == moveFingerID) moveTouch = i;
    86.                 else if (touches[i].position.x > half)
    87.                 {
    88.                     if (touches[i].phase == TouchPhase.Began)
    89.                     {
    90.                         mobileJump = true;
    91.                     }
    92.                 }
    93.                    
    94.  
    95.                 if ((touches[i].position.x < half) && (!mobileMoveOriginSet) && (touches[i].phase == TouchPhase.Began))
    96.                 {
    97.                     mobileMoveOriginSet = true;
    98.                     moveFingerID = touches[i].fingerId;
    99.                     moveTouch = i;
    100.                     moveOrigin = touches[i].position;
    101.                     if (!gameFinished) { currJoystick = Instantiate(touchJoystick) as GameObject;
    102.                         currJoystick.transform.SetParent(GameObject.FindGameObjectWithTag("MainCanvas").transform);
    103.                         currJoystick.transform.position = new Vector3(moveOrigin.x, moveOrigin.y, 0f); }
    104.                 }
    105.             }
    106.  
    107.             if (mobileMoveOriginSet)
    108.             {
    109.                 Vector2 newPos = (touches[moveTouch].position - moveOrigin).normalized;
    110.  
    111.                 Horizontal = newPos.x;
    112.                 if (Mathf.Abs(Horizontal) < mobileJoystickDeadzone) Horizontal = 0;
    113.                 //else Horizontal = 1f;
    114.  
    115.                 Vertical = newPos.y;
    116.                 if (Mathf.Abs(Vertical) < mobileJoystickDeadzone) Vertical = 0;
    117.                 //else Vertical = 1f;
    118.             }
    119.             else if(moveTouch < touches.Length)
    120.             {
    121.                 if (touches[moveTouch].phase == TouchPhase.Ended || touches[moveTouch].phase == TouchPhase.Canceled)
    122.                     mobileMoveOriginSet = false;
    123.             }
    124.         }
    125.         else
    126.         {
    127.             mobileMoveOriginSet = false;
    128.             Horizontal = 0;
    129.             Vertical = 0;
    130.         }
    131.  
    132.         if(!mobileMoveOriginSet && currJoystick != null)
    133.         {
    134.             Destroy(currJoystick);
    135.             currJoystick = null;
    136.         }
    137.  
    138.         // Mobile Input End ----------
    139.  
    140.         // If no input then dont move
    141.         float curSpeed = 0;
    142.         if ((Horizontal != 0 || Vertical != 0) && !gameFinished)
    143.         {
    144.             curSpeed = speed;
    145.             isRunning = true;
    146.         }
    147.  
    148.         // Forward movement
    149.         curForward = transform.forward * curSpeed * Time.deltaTime;
    150.  
    151.         // Rotation Settings
    152.         float right = 0, up = 0;
    153.  
    154.         // Get input Horizontal
    155.         if (Horizontal == 0) right = 0;
    156.         else right = Horizontal > 0 ? 1f : -1f;
    157.  
    158.         // Get input Vertical
    159.         up = Vertical;
    160.  
    161.         // If no Horizontal input, set Vertical direction accordingly
    162.         if (right == 0 && up >= 0) up = 0;
    163.         else if (right == 0 && up < 0) up = 4;
    164.  
    165.         // Store last rotation for keeping it for next update
    166.         if ((Horizontal != 0 || Vertical != 0) && !gameFinished)
    167.             //prevEulers = new Vector3(0, (90f * right) + (45f * (right > 0 ? -1f : 1f) * up) + cam.rotation.eulerAngles.y, 0);
    168.             prevEulers = new Vector3(0, (90f * right) + (45f * (right > 0 ? -1f : 1f) * up) -90f, 0);
    169.  
    170.  
    171.         // Movement End -------
    172.  
    173.         // Double Jump -------
    174.  
    175.         if ((Input.GetButtonDown("Jump") || mobileJump) && !isGrounded && canDoubleJump && !gameFinished && !doJump)
    176.         //if ((Input.GetButtonDown("Jump") || mobileJump) && !isGrounded && canDoubleJump && !gameFinished)
    177.         {
    178.             if (!((Time.time - jumpStart) < timeBetweenJump))
    179.             {
    180.                 // Start Jump
    181.                 isGrounded = false;
    182.                 playerAnimator.SetTrigger("DoubleJump");
    183.                 // Remove any Y-Velocity then add jump velocity
    184.                 //curForward.y = 0;
    185.                 //playerRigidbody.AddForce(0, jumpSpeed, 0, ForceMode.Impulse);
    186.                 doJump = true;
    187.                 doDoubleJump = true;
    188.                 // Cannot double jump now
    189.                 canDoubleJump = false;
    190.  
    191.                 //audioPlayer.loop = false;
    192.                 //audioPlayer.clip = doubleJumpAudio;
    193.                 //audioPlayer.Play();
    194.             }
    195.  
    196.         }
    197.  
    198.         // Double Jump End -------
    199.  
    200.         // Jump -------
    201.  
    202.         if ((Input.GetButtonDown("Jump") || mobileJump) && isGrounded && !gameFinished && !doJump)
    203.         //if ((Input.GetButtonDown("Jump") || mobileJump) && !isSliding && isGrounded && !gameFinished)
    204.         {
    205.             // Start jump
    206.             isGrounded = false;
    207.             playerAnimator.SetTrigger("Jump");
    208.             // Remove any Y-Velocity then add jump velocity
    209.             //curForward.y = 0;
    210.             //playerRigidbody.AddForce(0, jumpSpeed, 0, ForceMode.Impulse);
    211.             doJump = true;
    212.             doDoubleJump = false;
    213.             // Can double jump now
    214.             canDoubleJump = true;
    215.  
    216.             jumpStart = Time.time;
    217.  
    218.             //audioPlayer.loop = false;
    219.             //audioPlayer.clip = jumpAudio;
    220.             //audioPlayer.Play();
    221.         }
    222.  
    223.         // Jump End -------
    224.  
    225.         curForward.y = 0;
    226.         // Setting properties Finally -------
    227.         transform.rotation = Quaternion.Euler(prevEulers);
    228.         playerRigidbody.MovePosition(transform.position + curForward);
    229.         playerAnimator.SetBool("isRunning", isRunning);
    230.         playerAnimator.SetBool("isGrounded", isGrounded);
    231.     }
    232.  
    233.     void FixedUpdate()
    234.     {
    235.  
    236.         if (doJump && !gameFinished)
    237.         {
    238.             playerRigidbody.velocity = new Vector3(playerRigidbody.velocity.x, 0, playerRigidbody.velocity.z);
    239.             playerRigidbody.AddForce(0, jumpSpeed, 0, ForceMode.Impulse);
    240.  
    241.             audioPlayer.loop = false;
    242.             audioPlayer.clip = (doDoubleJump)?((doubleJumpAudio)?doubleJumpAudio:null):((jumpAudio)?jumpAudio:null);
    243.             if(audioPlayer.clip != null) audioPlayer.Play();
    244.  
    245.             doJump = doDoubleJump = false;
    246.         }
    247.  
    248.     }
    249.  
    250.     void OnDrawGizmos()
    251.     {
    252.         Gizmos.color = Color.red;
    253.         Gizmos.DrawWireCube(groundCheckPos.position, new Vector3(gcBoxWidth, gcBoxHeight, gcBoxLength));
    254.     }
    255. }
    Please help me i really can't figure out what the problem is.
    I'm really greatful to you all... TYSM
     
  10. iamfeyre

    iamfeyre

    Joined:
    May 20, 2016
    Posts:
    49
    PS - the rigidbody of player has 40 mass and 0 drag...
    also i tried moving player with Rigidbody.velocity also... but that gives even worse results..
     
  11. iamfeyre

    iamfeyre

    Joined:
    May 20, 2016
    Posts:
    49
  12. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    5,227
    Rigidbody.MovePosition is intended to be used from FixedUpdate, not Update. If you have Rigidbody.interpolation set to Interpolate, that should do a smooth movement to the new position on frames between calls to FixedUpdate.

    I'm not sure how this behaves when called from Update, especially if you get multiple MovePosition calls from multiple Update calls on the same object before the physics update, but I wouldn't be surprised if you were getting some unexpected movement behavior.
     
  13. iamfeyre

    iamfeyre

    Joined:
    May 20, 2016
    Posts:
    49
    I tried using it in FixedUpdate too.. the only difference is that in fixedupdate the input feels a little laggy...
     
  14. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    5,227
    Well, you don't want to take your input in FixedUpdate. You take your input in Update, and apply rigidbody movement in FixedUpdate. Rigidbody movement is applied during the physics update, which immediately follows FixedUpdate (it won't be applied any faster if moving it to Update). FixedUpdate is generally unreliable for taking user input though, because the Input class is designed to be called from Update and only provides the input that has come in since the last frame. Since there can be multiple frames between each call to FixedUpdate, if you just moved everything from your current Update over to FixedUpdate some data from the Input class will be lost.

    So, do everything with the Input class in Update, and do everything involving the Rigidbody in FixedUpdate.
     
    Last edited: Nov 5, 2018
  15. iamfeyre

    iamfeyre

    Joined:
    May 20, 2016
    Posts:
    49
    That's what i meant previously... ofcourse i always take input in Update only.. never changed that.. but moving rigidbody and applying forces... i tried using them in both Update and FixedUpdate.. still same problem...