Need to make a moving platform

So I need to make a moving platform that parents the player and lets me jump and move and all that, but I cant figure it out. Here’s my code.

MOVING PLATFORM

public class MovingPlatform : MonoBehaviour
{
    public Vector3 Pos1;
    public Vector3 Pos2;
    public float speed = 1f;

    void Update()
    {
        transform.position = Vector3.Lerp(Pos1, Pos2, Mathf.PingPong(Time.time * speed, 1f));
    }
}

PLAYER

public class PlayerController : MonoBehaviour
{
    public float speed;
    public float jumpforce;
    public float checkradius;
    public LayerMask whatisground;
    public Transform groundcheck;
    public int extrajumpsvalue;
    public float hangtime = 0.2f;
    public float jumpbufferlength = 0.1f;
    public bool facingright = true;
  
    private float jumpbuffercount;
    private float hangcount;
    private int extrajumps;
    private float moveinput;
    public bool isgrounded;
    private Rigidbody2D rb;
  
  

    // Start before first frame
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        extrajumps = extrajumpsvalue;
    }

    // updates every fixed frame
    void FixedUpdate()
    {
        isgrounded = Physics2D.OverlapCircle(groundcheck.position, checkradius, whatisground);
      
        //Moving Left And Right
        moveinput = Input.GetAxis("Horizontal");
        rb.velocity = new Vector2(moveinput * speed, rb.velocity.y);

        if(facingright == false && moveinput > 0)
        {
            Flip();
        }
        else if(facingright == true && moveinput < 0)
        {
            Flip();
        }
  
    }

  
    // Update once per frame
    void Update()
    {

        //Hangtime
       if(isgrounded)
        {
            hangcount = hangtime;
        }
        else
        {
            hangcount -= Time.deltaTime;
        }

       //Jump Buffer
       if(Input.GetButtonDown("Jump"))
        {
            jumpbuffercount = jumpbufferlength;
        }
       else
        {
            jumpbuffercount -= Time.deltaTime;
        }

        Vector2 jumpDir = Vector2.up;
        if (rb.gravityScale == -6)
        {
            jumpDir = -jumpDir;
        }

        if (isgrounded == true )
        {
            extrajumps = extrajumpsvalue;
        }


        if (Input.GetButtonDown("Jump") && extrajumps > 0)

        {
            rb.velocity = jumpDir * jumpforce;
            extrajumps--;
        }

        else if (jumpbuffercount >= 0 && extrajumps == 0 && hangcount > 0f)
        {
            rb.velocity = jumpDir * jumpforce;
        }
 
        if(Input.GetButtonUp("Jump") && rb.velocity.y > 0)
        {
            rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f);
        }
    }

    void Flip()
    {
        facingright = !facingright;
        Vector3 Scaler = transform.localScale;
        Scaler.x *= -1;
        transform.localScale = Scaler;
    }





}

bro are you just a god with all answers!?

No, im just really good at Googling things lol. Ive had a lot of issues in my game dev journey and eventually you get really good at searching online for the solutions.

1 Like

see the thing is I’ve looked up a lot of videos, a big problem I’m having is I’m unable to jump when on the platforms, and I’m not sure how to fix it…

My suggestion probably wont be your favorite but it will help you out a lot in the long run. Follow this tutorial and it will give you a great foundation to build upon.

I still use this movement code as a foundation for 2D movement. Its very easy to build upon and works great.

neverminded I’m just not smart, I didn’t put a ground tag on my moving platform…

oh ya that is actually the first one I watched, and also another by a guy name Alvin roe, but thank you so much for helping me twice, it just gets so frustrating learning both unity ,and code ,and pixel animation

Welcome to game dev!

1 Like