How can I make a Super Punch Out style of movement?

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;

}

}

}

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: Using code tags properly

I imagine you would have a target position to be in, or target rotation: center, or left / right if you hold the key.

This can help you tween smoothly to various quantities (position or rotation, or both):

Smoothing movement between any two particular values:

You have currentQuantity and desiredQuantity.

  • only set desiredQuantity
  • the code always moves currentQuantity towards desiredQuantity
  • read currentQuantity for the smoothed value

Works for floats, Vectors, Colors, Quaternions, anything continuous or lerp-able.

The code: SmoothMovement.cs · GitHub

Another approach would be to use a tweening package line LeanTween, DOTween or iTween.

Awesome! Thank you! I’ll be sure to use code tags in the future!

1 Like