Sonntag, 12. April 2020

Unity - 2d movement and jumping and character facing to left, right direction (Variant 20200412)

There is a Player GameObject with a rigid body property and a 2d collider property.
In the hierarchy is a another gameobject as a child of Player GameObject. This chlid got only a 2d collider property.
The collider of the parent player game object checks the normal bounderies.
The collider of the child player object is only used to detect if the parent game object is grounded or not. It is not grounded, when it currently jumps.

(What is the trick to let the character face to the right (default) and then to the left by the movement to the left of course: Whenever the character changes the movement to the left, flip the value of gameObject.transform.localScale.x from +1 to -1 and the other way around by a multiplying with just * -1;)
Snippets (1 script of the parent and another one for this child object)

Important is only to allow jumping when the character is currently not jumping. That is because the collider detecting if the character is grounded or not is a few pixel large and it takes a couple of frames to leave this zone. If you are ignoring to check the flag if it is currently jumping you will get a inconstince result with various jump heights!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerCode : MonoBehaviour
{
    public float PlayerSpeed = 5f;
    public float PlayerJumpForce = 0.6f;
    public bool isGrounded = false;

    private bool IsJumping = false;
    private bool isIdle = true;
    private bool isMoving = false;
    private float lockRotation = 0f;
    private bool isFacingRight = false;

    // Update is called once per frame
    void Update()
    {
        move();
        jump();
        gameObject.transform.rotation = Quaternion.Euler(lockRotation, lockRotation, lockRotation);
         
    }

    private void move()
    {
        // @see https://docs.unity3d.com/ScriptReference/Input.GetAxis.html
        // @see https://www.youtube.com/watch?v=L6Q6VHueWnU
        float horizontalMovementRate = Input.GetAxis("Horizontal");
        if (horizontalMovementRate != 0)
        {
            isMoving = true;
            Vector3 movement = new Vector3(horizontalMovementRate, 0f, 0f);
            transform.position += movement * Time.deltaTime * PlayerSpeed;
            if (horizontalMovementRate>0 & !isFacingRight ||       horizontalMovementRate<0 & isFacingRight)
            {
                isFacingRight = !isFacingRight;
                Vector3 newScale = transform.localScale;
                newScale.x = newScale.x * -1;
                transform.localScale = newScale;

            }
        }
        else
            isMoving = false;
    }


    void jump()
    {
        if (Input.GetButton("Jump") & isGrounded & !IsJumping)
        {
            gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, PlayerJumpForce), ForceMode2D.Impulse);
            IsJumping = true;
        }
    }
}

child object script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GroundedCode : MonoBehaviour
{
    GameObject Player;

    // Start is called before the first frame update
    void Start()
    {
        Player = gameObject.transform.parent.gameObject;
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.collider.tag == "Ground")
        {
            Player.GetComponent<PlayerCode>().isGrounded = true;
Player.GetComponent<PlayerCode>().IsJumping = false;
        }
    }

    private void OnCollisionExit2D(Collision2D collision)
    {
        if (collision.collider.tag == "Ground")
        {
            Player.GetComponent<PlayerCode>().isGrounded = false;
        }
    }

}





0 Kommentare:

Kommentar veröffentlichen

Abonnieren Kommentare zum Post [Atom]

<< Startseite