How to check ground for unity2d platformer game

微笑、不失礼 提交于 2020-12-15 04:59:05

问题


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

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.


回答1:


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)

EDIT:

try this:

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 acctually 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.

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

(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*/))

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

hope it helped :)



来源:https://stackoverflow.com/questions/58355913/how-to-check-ground-for-unity2d-platformer-game

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!