Search Unity

Question Unable to Jump while on a horizontally moving platform

Discussion in '2D' started by roastincoffee1978, May 11, 2023.

  1. roastincoffee1978

    roastincoffee1978

    Joined:
    Jan 22, 2020
    Posts:
    33
    Hoping to get some help with this. As I am relatively new to Unity Scripting, I am not really sure what is keeping me from being able to jump while on a moving platform.

    My player is able to move well on stationary objects. The player can also move left and right just fine while on a moving platform. However, I am not able to jump while on a moving platform.

    Here is my "StickPlatform" script:

    Code (CSharp):
    1. public class StickPlatform : MonoBehaviour
    2. {    
    3.     {      
    4.         if (collision.gameObject.name == "Player")
    5.         {
    6.              collision.gameObject.transform.SetParent(transform);
    7.         }
    8.     }
    9.  
    10.    
    11.     private void OnTriggerExit2D(Collider2D collision)
    12.     {
    13.        
    14.         if (collision.gameObject.name == "Player")
    15.         {
    16.             collision.gameObject.transform.SetParent(null);
    17.         }
    18.     }
    19. }
    Here is the "WaypointFollower" script that moves the platform:

    Code (CSharp):
    1. public class WaypointFollower : MonoBehaviour
    2. {
    3.  
    4.     [SerializeField] private GameObject[] waypoints;
    5.    
    6.     private int currentWaypointIndex = 0;
    7.  
    8.     [SerializeField] private float speed = 2f;
    9.    
    10.     private void Update()
    11.     {
    12.  
    13.         if (Vector2.Distance(waypoints[currentWaypointIndex].transform.position, transform.position) < .1f)
    14.         {
    15.             currentWaypointIndex++;
    16.  
    17.             if(currentWaypointIndex >= waypoints.Length)
    18.             {
    19.                 currentWaypointIndex = 0;
    20.             }
    21.         }
    22.  
    23.         transform.position = Vector2.MoveTowards(transform.position, waypoints[currentWaypointIndex].transform.position, Time.deltaTime * speed);
    24.     }
    25. }
    Thanks!
     
  2. Chubzdoomer

    Chubzdoomer

    Joined:
    Sep 27, 2014
    Posts:
    132
    Have you already tried using Debug.Log to make sure your Jump() method is being called? Perhaps while on the platform your character is never registered as being grounded. It really could be a number of things.
     
  3. roastincoffee1978

    roastincoffee1978

    Joined:
    Jan 22, 2020
    Posts:
    33
    Not sure how to show this in the debug log. The Jump method is in the Player Movement script...which isnt tied to the Platform Object at all.

    Edit: See the Player Movement script below
     
  4. roastincoffee1978

    roastincoffee1978

    Joined:
    Jan 22, 2020
    Posts:
    33
    Code (CSharp):
    1. public class PlayerMovement : MonoBehaviour
    2. {
    3.  
    4.     private Rigidbody2D rigidBody;
    5.     private BoxCollider2D collider;
    6.     private SpriteRenderer sprite;
    7.     private Animator animator;
    8.  
    9.     [SerializeField] private LayerMask jumpableGround;
    10.  
    11.     [SerializeField]private float moveSpeed = 7f; //sets the speed of the horizontal movement. [SerializedField] will show this property in Unity Editor
    12.     [SerializeField] private float jumpForce = 14f; //sets the force of the jump action. [SerializedField] will show this property in Unity Editor  
    13.     private float playerMoveX = 0f; //Allows Player to Move Left and Right (Horizontally)
    14.  
    15.     private enum MovementState { idle, running, jumping, falling  }
    16.    
    17.  
    18.     // Start is called before the first frame update
    19.     private void Start()
    20.     {
    21.         rigidBody = GetComponent<Rigidbody2D>();
    22.         sprite = GetComponent<SpriteRenderer>();
    23.         animator = GetComponent<Animator>();
    24.         collider = GetComponent<BoxCollider2D>();
    25.  
    26.     }
    27.  
    28.     // Update is called once per frame
    29.     private void Update()
    30.     {
    31.  
    32.         playerMoveX = Input.GetAxisRaw("Horizontal"); //GetAxisRaw gets back to 0 immediately.
    33.         //Player Move Speed (vector 2 (x = value of playerMoveX variable,
    34.         //y = whatever the y location is at any given time
    35.         rigidBody.velocity = new Vector2(playerMoveX * moveSpeed, rigidBody.velocity.y);
    36.  
    37.         //Allows Player to Jump
    38.         if (Input.GetButtonDown("Jump") && IsGrounded())
    39.         {
    40.  
    41.             rigidBody.velocity = new Vector2(rigidBody.velocity.x, jumpForce); //Vector2 is X and Y (2 vectors)
    42.         }
    43.  
    44.         UpdateAnimationState();
    45.  
    46.     }  
    47.  
    48.     private void UpdateAnimationState()
    49.     {
    50.  
    51.         MovementState state;
    52.             //Check to see if the player is running or is idle by checking the
    53.             //value of the playerMoveX value which controls the horizontal position of the player
    54.             if (playerMoveX > 0f)
    55.             {
    56.                 state = MovementState.running;
    57.                 sprite.flipX = false;
    58.             }
    59.             else if (playerMoveX < 0)
    60.             {
    61.             state = MovementState.running;
    62.             sprite.flipX = true;
    63.             }
    64.  
    65.             else
    66.  
    67.             {
    68.             state = MovementState.idle;
    69.             }
    70.  
    71.             if (rigidBody.velocity.y > .1f)
    72.             {
    73.  
    74.             state = MovementState.jumping;
    75.  
    76.             }
    77.  
    78.             else if (rigidBody.velocity.y < -.1f)
    79.             {
    80.                 state = MovementState.falling;
    81.             }
    82.  
    83.  
    84.             animator.SetInteger("state", (int)state);
    85.         }
    86.  
    87.     private bool IsGrounded()
    88.     {
    89.         //returns a TRUE or FALSE
    90.         //boxcast creates a new box
    91.         //the first two arguments "collider.bounds.center" and "collider.bounds.size" are where the box is drawn
    92.         //"0f" is the rotation of the box (no rotation set)
    93.         //Vecter2.down and .1f will offset the new box down by .1
    94.         //the last argument is the object ("jumpableGround") we are looking for to see if the new box is touching it
    95.         return Physics2D.BoxCast(collider.bounds.center, collider.bounds.size, 0f, Vector2.down, .1f, jumpableGround);
    96.     }
     
  5. roastincoffee1978

    roastincoffee1978

    Joined:
    Jan 22, 2020
    Posts:
    33
    Nevermind. I figured it out. LOL. I had to apply the "Ground" layer to the moving platform. I am dumb. LOL