Обнаружение земли в скрипте Unity Movement не работает - PullRequest
0 голосов
/ 19 июня 2020

Мне нужна помощь с моим проектом единства. Я новичок в разработке игр и решил использовать грубую силу в игре про марио, чтобы изучить некоторые основы. Однако я увлекся созданием сценария движения. Я с легкостью понял, как заставить моего персонажа Марио go слева и справа, но у меня проблемы с прыжками. Я могу прыгать, но Я ПРОСТО НЕ МОГУ СОЗДАТЬ, КАК ДЕЛАТЬ НАЗЕМНОЕ ОБНАРУЖЕНИЕ. Я бы включил какой-нибудь пример кода, но я столько всего пробовал, что даже не знаю, что бы включить. Пожалуйста, найдите время, чтобы помочь мне, вставив ссылку на копию вашего сценария движения, так что я надеюсь с этим справиться. Спасибо!

Изменить: он все еще не работает, и я действительно не знаю, почему Вот мой код:

using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using UnityEngine;

public class Mario : MonoBehaviour
{
    Rigidbody2D rb;
    Animator animator;
    SpriteRenderer spriterenderer;

    bool pushingButton;
    bool isGrounded;
    [SerializeField]
    float speed;
    [SerializeField]
    float jumpForce;



    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        animator = GetComponent<Animator>();
        spriterenderer = GetComponent<SpriteRenderer>();
    }



    void FixedUpdate()
    {
        isGrounded = Physics2D.Linecast(_transform.position, GroundChecker.postion, whatIsFloor);




        if (Input.GetKey("right") || Input.GetKey("left") || Input.GetKey("up"))
        {
            pushingButton = true;
        }

        else
            pushingButton = false;





        if (Input.GetKey("left") && Input.GetKey("right"))
        {
            animator.Play("Mario Idle");
            rb.velocity = new Vector2(0, 0);
        }

        else if (Input.GetKey("right") && Input.GetKey("up"))
        {
            animator.Play("Mario Jump");
            spriterenderer.flipX = false;
            rb.velocity = new Vector2(speed, rb.velocity.y);
        }

        else if (Input.GetKey("right"))
        {
            animator.Play("Mario Walk");
            spriterenderer.flipX = false;
            rb.velocity = new Vector2(speed, rb.velocity.y);
        }


        if (Input.GetKey("left") && Input.GetKey("right"))
        {
            animator.Play("Mario Idle");
            rb.velocity = new Vector2(0, 0);
        }

        else if (Input.GetKey("left") && Input.GetKey("up"))
        {
            animator.Play("Mario Jump");
            spriterenderer.flipX = true;
            rb.velocity = new Vector2(-speed, rb.velocity.y);
        }
        else if (Input.GetKey("left"))
        {
            animator.Play("Mario Walk");
            spriterenderer.flipX = true;
            rb.velocity = new Vector2(-speed, rb.velocity.y);
        }

        if (Input.GetKeyDown("up") && isGrounded)
        {
            animator.Play("Mario Jump");
            rb.velocity = new Vector2(rb.velocity.x, jumpForce);
            isGrounded = false;
        }

        if (pushingButton == false)
        {
            animator.Play("Mario Idle");
            rb.velocity = new Vector2(0, rb.velocity.y);
        }
    }
}

1 Ответ

1 голос
/ 19 июня 2020

создайте игровой объект «GroundCheck», сделайте его дочерним по отношению к персонажу и поместите его ниже ног персонажа // LayerMask, чтобы определить, что считается землей для игрока. Publi c LayerMask whatIsGround;

// Transform just below feet for checking if player is grounded
public Transform groundCheck;

void Update()
{

    /* Check to see if character is grounded by raycasting from the middle of the player down to the groundCheck position and see if collected with gameobjects on the whatIsGround layer */

    isGrounded = Physics2D.Linecast(_transform.position, groundCheck.position, whatIsGround);

    // If grounded AND jump button pressed, then allow the player to jump

    if (isGrounded && CrossPlatformInputManager.GetButtonDown("Jump")) 
    {
        DoJump();
    }
}
...