I’m try make a game with jumping like flappy bird, but there are platforms falling from the sky. If the player hits one of the platforms they will move the other way. My issue is that this is a slight jitter when moving up and down. This is what I have so far.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerMovement : MonoBehaviour
{
public float speedRight = 2f;
public float speedLeft = -2f;
public float jumpForce = 10f;
public bool movingLeft = false;
private Rigidbody2D rb;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
SideToSide();
if (Input.GetMouseButtonDown(0))
{
Jump();
}
}
private void SideToSide()
{
if (movingLeft == false)
{
var x_auto = Time.deltaTime * speedRight;
transform.Translate(x_auto, 0, 0);
}
if (movingLeft == true)
{
var x_auto = Time.deltaTime * speedLeft;
transform.Translate(x_auto, 0, 0);
}
}
private void Jump()
{
rb.velocity = Vector2.up * jumpForce;
}
private void OnTriggerEnter2D(Collider2D other)
{
if (movingLeft == true)
{
movingLeft = false;
}
else
{
if (movingLeft == false)
{
movingLeft = true;
}
}
}
}