如何检查Unity2D Platformer游戏的地面 [英] How to check ground for unity2d platformer game

查看:68
本文介绍了如何检查Unity2D Platformer游戏的地面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个2d平台,您可以从侧面看到玩家.我希望他不断前进,您必须在正确的时间按下空格,这样他才不会摔倒.现在一切正常,但他没有与地面碰撞.我希望它就像他在墙后奔跑一样,所以我想忽略我制作的某个图层,并与下面的框碰撞.到目前为止,我已经尝试了射线投射,观看了多个教程,并且进行了盒碰撞.盒式碰撞虽然有效,但要使所有平台都算是可靠的,我需要50个盒式对撞机.这是我当前的代码:

I am trying to make a 2d plat-former where you see the player from the side. I want him to be continuously moving and you have to press space at the right time so he doesn't fall. Right now everything works but he doesn't collide with the ground. I want it to be like he's running behind a wall so I want to ignore a certain layer I have made and collide with the boxes below that. So far I have tried ray casting, watched multiple tutorials, and did box collisions. Box collisions worked but to get all the platforms counted as solid I'd need like 50 box colliders. Here is my current code:

public int playerSpeed = 10;
    public int playerJumpPower = 1250;
    public float moveX;
    public float playerYSize = 2;
    public LayerMask mainGround;
    public float playerFallSpeed = 5;

    void Awake(){

    }

    // Update is called once per frame
    void Update()
    {
        RaycastHit2D hit = Physics2D.Raycast(transform.position, new Vector2(10, 0));
        if(hit.distance < 0.7f){
            print("hi");
        }

        Vector3 characterTargetPosition = new Vector3(transform.position.x + playerSpeed, transform.position.y, transform.position.z);
        transform.position = Vector3.Lerp(transform.position, characterTargetPosition, playerSpeed * Time.deltaTime);

        if(Input.GetKeyDown("space")){
            // float playerTargetPosY = transform.position.y + playerJumpPower;
            // Vector3 characterTargetPosition = new Vector3(transform.position.x, playerTargetPosY, transform.position.z);
            // transform.position = Vector3.Lerp(transform.position, characterTargetPosition, playerJumpPower * Time.deltaTime);

            gameObject.GetComponent<Rigidbody2D>().AddForce(Vector2.up * playerJumpPower);
        }
        //PlayerMove();
    }

我的播放器上有一个苯乙烯模型2D,所以现在他只是跌倒在地,但跳跃确实起作用.如果有任何简单的方法可以做到这一点.像一些脚本,教程或网站一样,我也欢迎您.请帮忙.

I have a rigidBody2D on my player so right now he just falls through the ground but the jump does work. If there is any easy way to do this. Like some script, a tutorial, or website I'm open for it. Please help.

推荐答案

播放器中是否有Rigidbody2D?要移动的东西通常必须具有RigidBody

Do you have a Rigidbody2D in your player? Things that will move usually have to have a RigidBody

(很抱歉,将其发布为答案.暂时还不能发表评论)

(sorry for posting this as an answer. Cant comment yet)

尝试一下:

Rigidbody2D rb;

void Awake()
{
    rb = GetComponent<Rigidbody2D>();
}

//Physics usually are done in FixedUpdate to be more constant
public void FixedUpdate(){
    if (Input.GetKeyDown("space"))
    {
        if(!rb.simulated)
            //player can fall
            rb.simulated = true;

        rb.AddForce(Vector2.up * playerJumpPower);
    }
    else
    {
        //third argument is the distance from the center of the object where it will collide
        //therefore you want the distance from the center to the bottom of the sprite
        //which is half of the player height if the center is actually in the center of the sprite
        RaycastHit2D hit = Physics2D.Raycast(transform.position, -Vector2.up, playerYSize / 2);

        if (hit.collider)
        {
            //make player stop falling
            rb.simulated = false;
        }
    }
}

如果玩家是唯一会与某物体发生碰撞的物体,则只需从该玩家不会与之碰撞的物体中取出碰撞器即可.

If the player is the only thing that will collide with something you can just take out the colliders from the object that the player will not collide with.

否则,您可以使用 hit.collider.gameObject.layer 检查碰撞对象的层,并确定玩家是否会与该层碰撞

Else you can check for the layer of the collided object with hit.collider.gameObject.layer and decide if the player will collide with that layer or not

(请注意,您必须与图层索引进行比较.如果要按索引名称获取索引,则可以使用 LayerMask.NameToLayer(/*图层名称*/))

(note that you have to compare with the index of the layer. If you want to get the index by its name you can use LayerMask.NameToLayer(/*layer name*/))

每次要使用RigidBody(例如AddForce())做某事时,您都必须做 rb.simulated = true

you will have to do rb.simulated = true everytime you want to do something with the RigidBody (like AddForce())

希望它有帮助:)

这篇关于如何检查Unity2D Platformer游戏的地面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆