A
A
Artem7777642020-08-17 22:35:00
C++ / C#
Artem777764, 2020-08-17 22:35:00

How to solve this jump bug in Unity?

I'm new to both Unity and Habré. Recently I decided to make a game without knowing anything (I'm learning on the go and adding). The game is a simple empty platformer. Couldn't get far. Somehow I did the character control, but there is a jamb that does not suit me.
The platform has the "Ground" tag.
The jump is performed on the "Space" key only if it has touched the platform ("Ground" tag).
The problem is that the character jumps not only when he is on the platform. He can jump whenever the character touches the platform. For example, I touched the platform from the side. I should fall, but I can jump (according to the logic of my game, you can only jump from the platform itself, so this should not be the case).
Is there any way to fix this? If yes, please tell me as much as possible, so that even such as I understood;)
Ps The script code is below.

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

public class MoveScript : MonoBehaviour
{
    private bool PlayerOnGround;
    private float Speed = 0.1f;
    private float Force = 200f;
    private Rigidbody2D Rigidbody2D;
    void Start()
    {
        Rigidbody2D = GetComponent<Rigidbody2D>();
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if(collision.transform.tag == "Ground")
        {
            PlayerOnGround = true;
            Debug.Log("It`s ground");
        }
    }
    private void OnCollisionExit2D(Collision2D collision)
    {
        if(collision.transform.tag == "Ground")
        {
            PlayerOnGround = false;
            Debug.Log("It`s air");
        }
    }
    void Update()
    {
        
    }

    private void FixedUpdate()
    {
        if (Input.GetKey(KeyCode.A))
        {
            transform.Translate(Vector3.left * Speed);
        }
        if (Input.GetKey(KeyCode.D))
        {
            transform.Translate(Vector3.right * Speed);
        }
        if (Input.GetKey(KeyCode.Space))
        {
            if(PlayerOnGround == true)
            {
                Rigidbody2D.AddForce(Vector3.up * Force);
            }
        }
    }
}

Answer the question

In order to leave comments, you need to log in

4 answer(s)
G
GavriKos, 2020-08-17
@Artem777764

Trite - make the platform collider only on top.
Or specify in terms of the unit what is "when the character is only on the platform"

A
Artem @Jump, 2015-05-01
Tag

How does is called?
This is called server administration. Working with the console.
And if you can, then leave good literature or video tutorials on this case.
google will help you.

D
Dmitry Aitkulov, 2015-05-02
@Scarfase1989

on you a reference )) I myself learned to install, the brain pumps well. You will hardly find a video (yes, they somehow depress) just sit and do it yourself. Google commands, read articles. Then, when you launch the panel, you can create 2-3 servers and transfer the DNS database and mail there and tie everything into one. And practice practice practice.

E
Eugene Burmakin, 2015-05-01
@Freika

System administration, console basics, DevOps (but this still affects programming to some extent).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question