Search Unity

Movement Programming

Discussion in '2D' started by Middrel, Oct 3, 2019.

  1. Middrel

    Middrel

    Joined:
    Oct 19, 2016
    Posts:
    72
    Hello Goodnight.

    I have a question about programming player movements. When my character jumps, he sometimes "spins" and falls "lying down" instead of getting firm to the floor or platform.

    Also, when it falls down a cliff, it spins in the air, and therefore falls back down.

    You can see the behavior in the video linked to this topic.



    The ideal behavior should be like when Samus (Metroid) or Mario (Super Mario) jump and fall "standing up", they don't go straight. Like the impulse exceeds my character.

    Similarly, on a sloping floor, my character instead of going down "correctly", rolls down.

    In the following video, we can see Samus correctly lower sloping floors, fall vertically in the cliffs and jump correctly.



    As you can see, it is a testing version that I am trying to improve. Here my code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CharacterMovement : MonoBehaviour
    6. {
    7.     Rigidbody2D rbd2;
    8.  
    9.     /*
    10.      *  Movimientos básicos.
    11.      */
    12.  
    13.     public float Speed = 10.0f;
    14.     public float jumpForce = 300.0f;
    15.     public bool facingRight = true;
    16.        
    17.     /*
    18.      *  Verificamos si el personaje está en contacto con el suelo.
    19.      */
    20.  
    21.     public Transform groundCheck;
    22.     public float groundRadius = 0.2f;
    23.     public bool grounded = false;
    24.     public LayerMask whatIsGround;
    25.    
    26.     // Start is called before the first frame update
    27.     void Start()
    28.     {
    29.        
    30.     }
    31.  
    32.     // Update is called once per frame
    33.     void FixedUpdate()
    34.     {
    35.         float move = Input.GetAxis("Horizontal");
    36.  
    37.         if (move < 0) GetComponent<Rigidbody2D>().velocity = new Vector3(move * Speed, GetComponent<Rigidbody2D>().velocity.y);
    38.         if (move > 0) GetComponent<Rigidbody2D>().velocity = new Vector3(move * Speed, GetComponent<Rigidbody2D>().velocity.y);
    39.  
    40.         if (move < 0 && facingRight) Flip();
    41.         if (move > 0 && !facingRight) Flip();
    42.  
    43.         bool jump = Input.GetButtonDown("Jump");
    44.  
    45.         grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
    46.  
    47.         if (jump && grounded) GetComponent<Rigidbody2D>().AddForce(Vector3.up * jumpForce);
    48.     }
    49.  
    50.     void Flip()
    51.     {
    52.         facingRight = !facingRight;
    53.         transform.Rotate(Vector3.up * 180);
    54.     }
    55. }
    56.  
    How I can fix these details?
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Try setting your Rigidbody2D component's constraints in the inspector, lock the Z rotation.

    If you would rather change it in code, you can use
    rb2d.constraints = RigidbodyConstraints2D.freezeRotation;


    In your code, you can assign
    rb2d
    once in the Start method using GetComponent<Rigidbody2D>(). Then everywhere else you can use
    rb2d
    variable instead of calling GetComponent each time.
     
    Middrel likes this.
  3. Middrel

    Middrel

    Joined:
    Oct 19, 2016
    Posts:
    72
    Ok! Thanks for answer jeffreyschoch!

    How is better? Change it in inspector or by code? Is there a difference or best practice?

    Regards!!
     
  4. Wezai

    Wezai

    Joined:
    Dec 30, 2016
    Posts:
    74
    No difference, you can change it in the inspector and be done with it. Unless if for some reason you want to unfreeze your character's Z constraint somewhere during runtime then you do it by code.
     
    Middrel and LiterallyJeff like this.
  5. Middrel

    Middrel

    Joined:
    Oct 19, 2016
    Posts:
    72
    Ok! Thanks jeffreyschoch, Wezai!! :D