Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question (still not resolved pls help)player physics dont look same when using 30fps and 350+fps

Discussion in '2D' started by kirigaya552, Feb 18, 2021.

  1. kirigaya552

    kirigaya552

    Joined:
    Jan 4, 2021
    Posts:
    22
    hello guys, i write this code and i was wondering about people who will play with a potato pc or a very good pc, so i tried to test the framerate issues, and there was lot of issues like player moving faster or player dashing in more long distance even player jumping higher when the fps are 350 +
    so i tried to move all the functions in fixedupdate , but now the player only jump when i press space 4 or 5 times, and can t dash properly
    thanks for reading and sorry for my english
    the script is below
     
    Last edited: Feb 18, 2021
  2. kirigaya552

    kirigaya552

    Joined:
    Jan 4, 2021
    Posts:
    22
    Code (CSharp):
    1. public class PlayerMovement : MonoBehaviour
    2. {
    3.     public float moveSpeed;
    4.     public float jumpForce;
    5.     public bool isGrouded;
    6.     public bool lookLeft;
    7.     public Transform feetPos;
    8.     public float checkRadius;
    9.     public LayerMask layerIsGrounded;
    10.     private float jumpTimeCounter;
    11.     public float jumpTime;
    12.     private bool isJumping;
    13.     private bool isDashbuttonDown;
    14.     public Rigidbody2D rb;
    15.     public SpriteRenderer sprite;
    16.     private int direction;
    17.     private float dashTime;
    18.     public float dashSpeed;
    19.     public float startDashTime;
    20.     public float firstDashing = 0f;
    21.     public float nextDashing = 1f;
    22.     public ParticleSystem dashEffect;
    23.  
    24.     private Animator anim;
    25.     private shake shake;
    26.     private void Awake()
    27.     {
    28.         Application.targetFrameRate = 60;
    29.     }
    30.     public void Start()
    31.     {
    32.         anim = GetComponent<Animator>();
    33.         rb = GetComponent<Rigidbody2D>();
    34.         dashTime = startDashTime;
    35.         shake  = GameObject.FindGameObjectWithTag("screenShake").GetComponent<shake>();
    36.         //currentStamina = maxStamina;
    37.         //staminaBar.setStamina(currentStamina);
    38.         //staminaBar.setMaxStamina(maxStamina);
    39.         currentMana = maxMana;
    40.         manaBar.setMana(currentMana);
    41.         manaBar.setMaxMana(maxMana);
    42.         currentPlayerHealth = maxPlayerHealth;
    43.         healthBar.setHealth(currentPlayerHealth);
    44.         healthBar.setMaxHealth(maxPlayerHealth);
    45.     }
    46.  
    47.     void Update()
    48.     {
    49.         isGrouded = Physics2D.OverlapCircle(feetPos.position, checkRadius, layerIsGrounded);
    50.         playerMove();
    51.         updatePositionDirection();
    52.         jumpAnimation();
    53.     dash();
    54.        
    55.     }
    56.  
    57.     public void updatePositionDirection()
    58.     {
    59.         float move = Input.GetAxis("Horizontal");
    60.  
    61.         if((move> 0f && lookLeft) || (move < 0f && !lookLeft))
    62.         {
    63.             lookLeft = !lookLeft;
    64.             transform.Rotate(new Vector3(0f,180f,0f));
    65.         }
    66.         if(move == 0)
    67.         {
    68.             anim.SetBool("isRun",false);
    69.         }else
    70.         {
    71.             anim.SetBool("isRun", true);
    72.          
    73.         }
    74.         if(move < -0.1f && isGrouded && !isJumping) FindObjectOfType<AudioManager>().Play("sfx_step_grass_l");
    75.         else if(move > 0.1f && isGrouded && !isJumping) FindObjectOfType<AudioManager>().Play("sfx_step_grass_r");
    76.     }
    77.     public void playerMove()
    78.     {
    79.         float move = Input.GetAxis("Horizontal");
    80.         Vector3 movement = new Vector3(move,0f,0f);
    81.         transform.position += movement * Time.deltaTime * moveSpeed;
    82.     }
    83.     public void jumpAnimation()
    84.     {
    85.         if(Input.GetKeyDown(KeyCode.Space) && isGrouded == true)
    86.         {
    87.             isJumping = true;
    88.             jumpTimeCounter = jumpTime;
    89.             rb.velocity = Vector2.up * jumpForce;
    90.         }
    91.         if(Input.GetKey(KeyCode.Space) && isJumping == true)
    92.         {
    93.             if(jumpTimeCounter > 0)
    94.             {
    95.                 rb.velocity = Vector2.up * jumpForce;
    96.                 jumpTimeCounter -= Time.deltaTime;
    97.             }else
    98.             {
    99.                 isJumping = false;
    100.             }
    101.         }
    102.         if(Input.GetKeyUp(KeyCode.Space))
    103.         {
    104.             isJumping = false;
    105.         }
    106.         if(isGrouded == true)
    107.         {
    108.             anim.SetBool("isJumping", false);
    109.         }
    110.         if(isGrouded == false)
    111.         {
    112.             if(anim.GetBool("isDash") == true)
    113.             {
    114.                 anim.SetBool("isDash", true);
    115.                 anim.SetBool("isJumping", false);
    116.             }else
    117.             {
    118.                 anim.SetBool("isJumping", true);
    119.             }
    120.          
    121.         }
    122.     }
    123.  
    124.     void dash()
    125.     {
    126.         if(direction == 0)
    127.         {
    128.  
    129.             if(Input.GetKeyDown(KeyCode.Mouse1)&& !lookLeft /*&& currentStamina >= 1*/ && firstDashing == 0f)
    130.             {
    131.                 direction= 1;
    132.                 anim.SetBool("isDash", true);
    133.                 createDash();
    134.                 shake.camShake();
    135.                 firstDashing = nextDashing;
    136.              
    137.                 //loseStamina(1);
    138.                 //staminaBar.setStamina(currentStamina);
    139.             }else if (Input.GetKeyDown(KeyCode.Mouse1) && lookLeft /*&& currentStamina >=1*/ && firstDashing == 0f)
    140.             {
    141.                 direction = 2;
    142.                 anim.SetBool("isDash", true);
    143.                 createDash();
    144.                 shake.camShake();
    145.                 firstDashing = nextDashing;
    146.              
    147.                 //loseStamina(1);
    148.                 //staminaBar.setStamina(currentStamina);
    149.             }
    150.         }else
    151.         {
    152.             if(dashTime <= 0)
    153.             {
    154.                 direction = 0;
    155.                 dashTime = startDashTime;
    156.                 rb.velocity = Vector2.zero;
    157.                 anim.SetBool("isDash", false);
    158.             } else
    159.             {
    160.                 dashTime -=   Time.deltaTime;
    161.                 if(direction == 1)
    162.                 {
    163.                     rb.velocity = Vector2.right * dashSpeed;
    164.                 }else if (direction == 2)
    165.                 {
    166.                     rb.velocity = Vector2.left * dashSpeed;
    167.                 }
    168.             }
    169.         }  
    170.     }