Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to make a movement system similar to super meat boy.

Discussion in 'Getting Started' started by Iqew, May 17, 2017.

  1. Iqew

    Iqew

    Joined:
    Nov 26, 2016
    Posts:
    38
    I'd like some help with my movement system. Currently, I'm making a game with inspired by games like Give Up and super meat boy. With that said, i want a movement system similar to those games. Basically I want it so that when I let go of a button, the player immediately stops or starts falling if it is mid-air or maybe do a double jump. At the moment this is all of my movement code.
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PlayerController : MonoBehaviour {
    4.  
    5.     public GameObject Player;
    6.  
    7.     public float speed;
    8.     public float JumpHeight;
    9.     //public float FallSpeed;
    10.  
    11.     private Rigidbody rb;
    12.     private Vector3 Velocity;
    13.     private bool movX;
    14.     private bool grounded;
    15.     private bool isFalling;
    16.    
    17.  
    18.     void Start () {
    19.         rb = Player.GetComponent<Rigidbody>();
    20.         Velocity = Player.GetComponent<Rigidbody>().velocity;
    21.         movX = false;
    22.        
    23.     }
    24.    
    25.    
    26.     void FixedUpdate () {
    27.         if(Velocity.y <= -0.1){
    28.             grounded = false;
    29.             isFalling = true;
    30.         }
    31.         if(Velocity.y >= 0.1){
    32.             grounded = false;
    33.             isFalling = false;
    34.         }
    35.         if(Velocity.y == 0){
    36.             grounded = true;
    37.         }
    38.  
    39.         //MoveRight
    40.         if(Input.GetKey("d") || Input.GetKey(KeyCode.RightArrow)){
    41.             rb.isKinematic = false;
    42.             rb.AddForce(10 * speed * Time.deltaTime, 0, 0);
    43.             //Velocity.x = 10 * speed * Time.deltaTime;
    44.             movX = true;
    45.         }
    46.         //MoveLeft
    47.         else if(Input.GetKey("a") || Input.GetKey(KeyCode.LeftArrow)){
    48.             rb.isKinematic = true;
    49.             rb.AddForce(-10 * speed * Time.deltaTime, 0, 0);
    50.             //Velocity.x = -10 * speed * Time.deltaTime;
    51.             movX = true;
    52.         }
    53.         else{
    54.             rb.isKinematic = true;
    55.         }
    56.        
    57.  
    58.         if(Input.GetKeyDown("w") && grounded == true || Input.GetKeyDown(KeyCode.UpArrow) && grounded == true){
    59.             rb.isKinematic = false;
    60.             rb.AddForce(0, 10 * JumpHeight * Time.deltaTime, 0);
    61.         }
    62.        
    63.        
    64.        
    65.        
    66.        
    67.     }
    68. }
    69.    
    70.  
    I know a lot of the code is wrong but i put it up here anyways so you guys can have some reference on the problem.
    Hopefully someone can help me.
     
  2. Donay

    Donay

    Joined:
    Apr 28, 2017
    Posts:
    70
    Start only runs once.
    Think you need to make the following chages

    Code (csharp):
    1.  
    2. void Start()
    3. {
    4.     rb = Player.GetComponent<Rigidbody>();
    5. }
    6.  
    7. void FixedUpdate() {
    8.     Velocity = rb.velocity;
    9.     movX = false;
    10.  
    To stop the player you can do somthing like
    Code (csharp):
    1.  
    2. rb.velocity = Vector3.zero;
    3. rb.angularVelocity = Vector3.zero;
    4.  
    but make sure it only runs once when they user stops pressing a key or the player will stop moving.
     
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    You might also enjoy this article, where I demonstrate how to make a run & jump system (including fine jump control and double-jump)
     
  4. PurpleOwl

    PurpleOwl

    Joined:
    Jul 22, 2019
    Posts:
    1
    Hi! I am new in unity and I have a question: Why in the if with key "d" isKinematic = false and in the if with the "a" key
    isKinematic = true?
     
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Yeah, that's pretty suspicious. I suspect that that's not really code you should be trying to learn from. Check out my article. :)
     
  6. TorkanlooSaleh

    TorkanlooSaleh

    Joined:
    Nov 11, 2022
    Posts:
    1
    Hi, i wrote several scripts and attached them to player gameobject to handle player movement like supermeatboy.
    i also applied some changes to the rigidbody2D of the player gameobject
     

    Attached Files:

  7. RichAllen2023

    RichAllen2023

    Joined:
    Jul 19, 2016
    Posts:
    1,026
    My Bolds.

    Watch yer spelling when writing code and stuff, it's super sensitive.