【问题标题】:Ground Detection in Unity Movement Script Won't WorkUnity 运动脚本中的地面检测不起作用
【发布时间】:2020-10-09 03:39:13
【问题描述】:

我的统一项目需要帮助。我是 gamedev 的新手,为了学习一些基础知识,我决定暴力破解马里奥游戏。然而,我已经开始制作一个动作脚本了。我很轻松地想出了如何让我的马里奥角色左右移动,但我的跳跃有问题。我可以跳,但我就是不知道如何进行地面探测。我会包含一些代码示例,但是我尝试了很多东西,我什至不知道我会包含什么。请花时间通过粘贴指向您的运动脚本副本的链接来帮助我,这样我就可以解决这个问题。谢谢!

编辑: 它仍然无法正常工作,我真的不知道为什么 这是我的代码:

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);
        }
    }
}

【问题讨论】:

  • 我们无法回答这个问题,除非您向我们展示您当前拥有的代码,无论代码有多损坏,因为我们不知道您是如何在游戏中实现玩家移动的。

标签: c# unity3d scripting game-development


【解决方案1】:

创建一个游戏对象“groundCheck”,使其成为角色的子对象并将其放置在角色脚下 // LayerMask 确定玩家认为什么是地面 public 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();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-03
    • 2018-05-11
    • 1970-01-01
    • 2015-09-23
    • 1970-01-01
    • 2015-08-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多