I’m looking to create a Super Punch Out style of movement where the character remains in one position, then can move quickly to the left or right when the left or right key is pressed, but return back to center when released. I’m new to C# and have been working with RigidBody2D. Here is my code right now which moves okay but not the Super Punch Out style that I want:
private Rigidbody2D rb2D;
private float moveSpeed;
private float jumpForce;
private bool isJumping;
private float moveHorizontal;
private float moveVertical;
private Transform startPosition;
private bool isMoving;
// Start is called before the first frame update
void Start()
{
rb2D = gameObject.GetComponent();
moveSpeed = 3f;
jumpForce = 60f;
isJumping = false;
startPosition = transform.position(x = -7.5, y = -1.45, z = 0);
isMoving = false;
}
// Update is called once per frame
void Update()
{
{
moveHorizontal = Input.GetAxisRaw(“Horizontal”);
moveVertical = Input.GetAxisRaw(“Vertical”);
}
if (!isMoving)
{
isMoving = true
}
}
void FixedUpdate()
{
if (moveHorizontal > 0.1f || moveHorizontal < -0.1f)
{
rb2D.AddForce(new Vector2(moveHorizontal * moveSpeed, 0f), ForceMode2D.Impulse);
}
if (!isJumping && moveVertical > 0.1f)
{
rb2D.AddForce(new Vector2(0f, moveVertical * jumpForce), ForceMode2D.Impulse);
}
}
void OnTriggerEnter2D(Collider2D collision)
{
if(collision.gameObject.tag == “Platform”)
{
isJumping = false;
}
}
void OnTriggerExit2D(Collider2D collision)
{
if (collision.gameObject.tag == “Platform”)
{
isJumping = true;
}
}
}