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;
}
}