Search Unity

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

Discussion in '2D' started by InkMoose, Jun 17, 2022.

  1. InkMoose

    InkMoose

    Joined:
    Jun 14, 2022
    Posts:
    5
    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<Rigidbody2D>();



    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;

    }

    }





    }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    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:

    https://forum.unity.com/threads/beginner-need-help-with-smoothdamp.988959/#post-6430100

    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: https://gist.github.com/kurtdekker/fb3c33ec6911a1d9bfcb23e9f62adac4

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

    InkMoose

    Joined:
    Jun 14, 2022
    Posts:
    5
    Awesome! Thank you! I’ll be sure to use code tags in the future!
     
    Kurt-Dekker likes this.