Search Unity

How can I get the player to stay on the moving platform? (2D platformer)

Discussion in 'Scripting' started by CoolKrit, Jan 12, 2019.

  1. CoolKrit

    CoolKrit

    Joined:
    Aug 21, 2018
    Posts:
    5
    How can I get the player to stay on the moving platform? Here is the code for the moving platform: HELP PLEASE!

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MovingPlatform : MonoBehaviour
    5. {
    6.     public Vector3 finishPos = Vector3.zero;
    7.     public float speed = 0.5f;
    8.  
    9.     private Vector3 _startPos;
    10.     private float _trackPercent = 0;
    11.     private int _direction = 1;
    12.  
    13.     // Use this for initialization
    14.     void Start()
    15.     {
    16.         _startPos = transform.position;
    17.     }
    18.  
    19.     void OnDrawGizmos()
    20.     {
    21.         Gizmos.color = Color.red;
    22.         Gizmos.DrawLine(transform.position, finishPos);
    23.     }
    24.  
    25.     // Update is called once per frame
    26.     void Update()
    27.     {
    28.         _trackPercent += _direction * speed * Time.deltaTime;
    29.         float x = (finishPos.x - _startPos.x) * _trackPercent + _startPos.x;
    30.         float y = (finishPos.y - _startPos.y) * _trackPercent + _startPos.y;
    31.         transform.position = new Vector3(x, y, _startPos.z);
    32.  
    33.         if ((_direction == 1 && _trackPercent > .9f) || (_direction == -1 && _trackPercent < .1f))
    34.         {
    35.             _direction *= -1;
    36.         }
    37.     }
    38. }



    And PlayerController script



    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class PlayerController : MonoBehaviour
    7. {
    8.  
    9.     public float speed;
    10.     public float jumpForce;
    11.     private float moveInput;
    12.  
    13.     private Rigidbody2D rb;
    14.  
    15.     private bool facingRight = true;
    16.  
    17.  
    18.     private bool isGrounded;
    19.     public Transform groundCheck;
    20.     public float checkRadius;
    21.     public LayerMask whatIsGround;
    22.  
    23.     public int extraJumps;
    24.     public int extraJumpsValue;
    25.  
    26.     private void Start()
    27.     {
    28.         extraJumps = extraJumpsValue;
    29.         rb = GetComponent<Rigidbody2D>();
    30.  
    31.     }
    32.  
    33.     private void FixedUpdate()
    34.     {
    35.         isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);
    36.  
    37.         moveInput = Input.GetAxis("Horizontal");
    38.         rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
    39.  
    40.         if (facingRight == false && moveInput > 0)
    41.         {
    42.             Flip();
    43.         }
    44.         else if (facingRight == true && moveInput < 0)
    45.         {
    46.             Flip();
    47.         }
    48.     }
    49.  
    50.     private void Update()
    51.     {
    52.         if (isGrounded == true)
    53.         {
    54.             extraJumps = extraJumpsValue;
    55.         }
    56.  
    57.         if (Input.GetKeyDown(KeyCode.Space) && extraJumps > 0)
    58.         {
    59.             rb.velocity = Vector2.up * jumpForce;
    60.             extraJumps--;
    61.         }
    62.         else if (Input.GetKeyDown(KeyCode.Space) && extraJumps == 0 && isGrounded == true)
    63.         {
    64.             rb.velocity = Vector2.up * jumpForce;
    65.         }
    66.     }
    67.  
    68.     void Flip()
    69.     {
    70.         facingRight = !facingRight;
    71.         Vector3 Scaler = transform.localScale;
    72.         Scaler.x *= -1;
    73.         transform.localScale = Scaler;
    74.     }
    75.  
    76.     private void OnTriggerEnter2D(Collider2D col)
    77.     {
    78.         if (col.tag == "Spike")
    79.         {
    80.             Scene scene;
    81.             scene = SceneManager.GetActiveScene();
    82.             SceneManager.LoadScene(scene.name);
    83.         }
    84.     }
    85. }
     
    Last edited: Jan 12, 2019
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    1,201
    @CoolKrit

    If you are moving your player with rb.velocity, you could keep track of passengers on each platform.

    When platform does it's FixedUpdate, add the velocity of platform to each passenger.

    When passenger is no longer touching the platform, remove it from this list.

    I can't remember all the details, but this is pretty much how it works.
     
    CoolKrit likes this.
  3. CoolKrit

    CoolKrit

    Joined:
    Aug 21, 2018
    Posts:
    5
    Sorry, but can you fixed my code or you can't?
     
  4. SparrowsNest

    SparrowsNest

    Joined:
    Apr 6, 2017
    Posts:
    1,249
    I can, how much are you willing to pay?
     
    eses likes this.