Search Unity

Resolved can jump in "Maximize On Play" preview only

Discussion in '2D' started by Bloodjoker666, May 8, 2023.

  1. Bloodjoker666

    Bloodjoker666

    Joined:
    Nov 26, 2020
    Posts:
    42
    Hi,
    my character (platformer game, 2d) can jump in "Maximize On Play" mode in preview screen but not in the windowed mode.

    First time I got this problem with a project.

    Any solutions?

    Thanks!
     
  2. MaxwellTan

    MaxwellTan

    Unity Technologies

    Joined:
    Mar 3, 2022
    Posts:
    75
    Hi Bloodjoker666, are able to provide a sample of your project? video or screenshot or the issue? unity version?
     
  3. Bloodjoker666

    Bloodjoker666

    Joined:
    Nov 26, 2020
    Posts:
    42
    Hi,
    Unity version: 2020.3.21f1, personal <DX11>

    Here are the scripts for player controller, and camera.

    PlayerController
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PlayerController : MonoBehaviour
    4. {
    5.     public GameObject ground;
    6.     public LayerMask collision;
    7.     public LayerMask created;
    8.     public float speed; // Vitesse de déplacement du personnage
    9.     public float jumpForce; // Force de saut du personnage
    10.    
    11.     private bool isGrounded = false; // Le personnage touche-t-il le sol ?
    12.  
    13.     private Rigidbody2D rb;
    14.  
    15.     public float maxVelocity;
    16.  
    17.     private float jumpTimeCounter;
    18.     public float jumpTime;
    19.     private bool isJumping;
    20.  
    21.     public bool isFacingRight;
    22.  
    23.     public int doubleJumpMax;
    24.     private int doubleJumpValue;
    25.  
    26.     private Animator anim;
    27.     private bool canHammer;
    28.  
    29.     void Start()
    30.     {
    31.         rb = GetComponent<Rigidbody2D>();
    32.         anim = GetComponent<Animator>();
    33.         speed = 10.0f;
    34.         maxVelocity = 5.0f;
    35.         isFacingRight = true;
    36.         doubleJumpMax = 1;
    37.         doubleJumpValue = doubleJumpMax;
    38.         //jumpForce = 0.5f;
    39.         //jumpTime = 0.8f;
    40.         canHammer = true;
    41.     }
    42.  
    43.     void Update()
    44.     {
    45.         // Vérifier si le personnage touche le sol
    46.         if (Physics2D.OverlapCircle(ground.transform.position, 0.5f, collision) || Physics2D.OverlapCircle(ground.transform.position, 0.5f, created))
    47.         {
    48.             isGrounded = true;
    49.         }
    50.         else
    51.         {
    52.             isGrounded = false;
    53.         }
    54.  
    55.         // Déplacement horizontal du personnage
    56.         float moveInput = Input.GetAxisRaw("Horizontal");
    57.         rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
    58.  
    59.         // Saut progressif
    60.         if (isGrounded == true && Input.GetButtonDown("A"))
    61.         {
    62.             isJumping = true;
    63.             anim.SetTrigger("Jump");
    64.             jumpTimeCounter = jumpTime;
    65.             //rb.velocity = Vector2.up * jumpForce;
    66.             rb.AddForce(new Vector2(0f, jumpForce), ForceMode2D.Impulse);
    67.         }
    68.  
    69.         if (doubleJumpValue > 0 && Input.GetButtonDown("A"))
    70.         {
    71.             isJumping = true;
    72.             anim.SetTrigger("Jump");
    73.             jumpTimeCounter = jumpTime;
    74.             rb.AddForce(new Vector2(0f, jumpForce), ForceMode2D.Impulse);
    75.             doubleJumpValue--;
    76.         }
    77.         /////
    78.         if (Input.GetButton("A") && isJumping == true)
    79.         {
    80.             if (jumpTimeCounter > 0)
    81.             {
    82.                 rb.AddForce(new Vector2(0f, jumpForce), ForceMode2D.Impulse);
    83.                 jumpTimeCounter -= Time.deltaTime;
    84.             }
    85.             else
    86.             {
    87.                 isJumping = false;
    88.             }
    89.         }
    90.        
    91.         if (Input.GetButtonUp("A"))
    92.         {
    93.             isJumping = false;
    94.         }
    95.  
    96.         if (Input.GetButton("B"))
    97.         {
    98.             if (canHammer)
    99.             {
    100.                 canHammer = false;
    101.                 anim.SetTrigger("Hit");
    102.             }
    103.         }
    104.  
    105.         if (isGrounded)
    106.         {
    107.             doubleJumpValue = doubleJumpMax;
    108.  
    109.             if (moveInput < 0.1f && moveInput > -0.1f)
    110.             {
    111.                 anim.SetBool("isIdling", true);
    112.                 anim.SetBool("isWalking", false);
    113.                 anim.SetBool("isRunning", false);
    114.             }
    115.             else
    116.             {
    117.                 if (moveInput < -0.5f || moveInput > 0.5f)
    118.                 {
    119.                     anim.SetBool("isRunning", true);
    120.                     anim.SetBool("isWalking", false);
    121.                     anim.SetBool("isIdling", false);
    122.                 }
    123.                 else
    124.                 {
    125.                     anim.SetBool("isWalking", true);
    126.                     anim.SetBool("isIdling", false);
    127.                     anim.SetBool("isRunning", false);
    128.                 }
    129.             }
    130.         }
    131.  
    132.         #region LIMIT VELOCITY
    133.         //limtation de la velocity
    134.         if (rb.velocity.sqrMagnitude > maxVelocity)
    135.         {
    136.             rb.velocity *= 0.99f;
    137.         }
    138.         #endregion
    139.         #region FLIP
    140.         if (isFacingRight == false && moveInput > 0)
    141.         {
    142.             Flip();
    143.         }
    144.         else if (isFacingRight == true && moveInput < 0)
    145.         {
    146.             Flip();
    147.         }
    148.         #endregion
    149.     }
    150.  
    151.     public void SetHammerTrue()
    152.     {
    153.         canHammer = true;
    154.     }
    155.     private void Flip()
    156.     {
    157.         Vector3 currentScale = transform.localScale;
    158.         currentScale.x *= -1;
    159.         gameObject.transform.localScale = currentScale;
    160.         isFacingRight = !isFacingRight;
    161.     }
    162. }
    163.  
    164.  
    Camera Controller
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class CameraFollow : MonoBehaviour
    4. {
    5.     public Transform player;
    6.     public float smoothTime = 0.3f;
    7.     public float xOffset = 3f;
    8.     public float yOffset = 2f;
    9.     public float joystickThreshold = 0.1f;
    10.     public float verticalSpeed = 2f;
    11.     public float cameraHeight = 5f; // Hauteur de la caméra par rapport au joueur
    12.  
    13.     private Vector3 velocity = Vector3.zero;
    14.  
    15.     void LateUpdate()
    16.     {
    17.         // Récupère la position actuelle de la caméra
    18.         Vector3 cameraPos = transform.position;
    19.  
    20.         // Calcul la position cible de la caméra
    21.         Vector3 targetPos = player.position;
    22.         targetPos.z = -10f; // Fixe la valeur Z de la position cible à -10 pour rester en 2D
    23.         targetPos.y += cameraHeight; // Ajoute la hauteur de la caméra à la position Y cible
    24.  
    25.         // Si le joueur est trop proche des limites horizontales de l'écran, on décale la cible de la caméra pour éviter qu'elle ne le suive de trop près
    26.         if (targetPos.x < cameraPos.x - xOffset || targetPos.x > cameraPos.x + xOffset)
    27.         {
    28.             targetPos.x = Mathf.Clamp(targetPos.x, cameraPos.x - xOffset, cameraPos.x + xOffset);
    29.         }
    30.  
    31.         // Si le joueur est trop proche des limites verticales de l'écran, on décale la cible de la caméra pour éviter qu'elle ne le suive de trop près
    32.         if (targetPos.y < cameraPos.y - yOffset || targetPos.y > cameraPos.y + yOffset)
    33.         {
    34.             targetPos.y = Mathf.Clamp(targetPos.y, cameraPos.y - yOffset, cameraPos.y + yOffset);
    35.         }
    36.  
    37.         /*
    38.         // Récupère l'input vertical du joystick
    39.         float verticalInput = Input.GetAxis("Vertical");
    40.  
    41.         // Si l'input vertical est supérieur à un seuil, déplace la caméra verticalement
    42.         if (Mathf.Abs(verticalInput) > joystickThreshold)
    43.         {
    44.             targetPos.y += verticalInput * verticalSpeed;// * Time.deltaTime;
    45.         }
    46.         */
    47.  
    48.         // Lisse le mouvement de la caméra jusqu'à la position cible
    49.         transform.position = Vector3.SmoothDamp(cameraPos, targetPos, ref velocity, smoothTime);
    50.     }
    51. }
    52.  
     
  4. Bloodjoker666

    Bloodjoker666

    Joined:
    Nov 26, 2020
    Posts:
    42
    @MaxwellTan after some tests, it appears that gravity of RigidBody2D don't apply the same way on windowed preview mode screen and Maximize On Play preview screen...

    Is there a way to make the gravity act exactly the same in both preview modes?

    Thanks!
    B
     
  5. Bloodjoker666

    Bloodjoker666

    Joined:
    Nov 26, 2020
    Posts:
    42
    problem is repeated with the same PlayerController code on a new project...

    so, the problem probably come from there

    B
     
  6. Bloodjoker666

    Bloodjoker666

    Joined:
    Nov 26, 2020
    Posts:
    42
    [EDIT: this NOT resolve the problem, see MelvMay post for the good answer] RESOLVED: by reseting Layouts (Window -> Layouts -> Reset All Layouts) !

    Thanks all!
    B
     
    Last edited: May 31, 2023
    MaxwellTan likes this.
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    To be clear here for anyone reading this, that's not true and cannot be true; Physics knows nothing about the Editor nor does it care about the rendering, frame-rate, window-size etc.

    Given that you're doing mouse-position and camera work which are affected by the screen-size (etc), that should be your first culprit.

    Note that you're applying forces/impulses per-frame. This can make your game frame-rate dependent. Physics runs (by default) at the FixedUpdate rate so this doesn't happen.

    Finally, there's a much easier and 100% accurate way to detect if you're grounded by asking the physics system if you're touching things. You can see that here and associated scripts:
     
    Bloodjoker666 and MaxwellTan like this.
  8. Bloodjoker666

    Bloodjoker666

    Joined:
    Nov 26, 2020
    Posts:
    42
    Hi,
    I must admit that I was totally wrong: your example fixed my problem.
    I'm sorry for posting a bad answer, which moreover doesn't work at all.
    Thank you so much! @MelvMay
    B
     
    Kurt-Dekker likes this.