0

我试图从这个实时会话中调整 2d 平台游戏角色控制器:https ://www.youtube.com/watch?v=wGI2e3Dzk_w&list=PLX2vGYjWbI0SUWwVPCERK88Qw8hpjEGd8

进入二维自上而下的角色控制器。它似乎有效,但有可能通过按下一些我无法真正找到的键组合进入对撞机,但它很容易实现。

问题是我不明白碰撞检测是如何在这里真正工作的,所以我不知道如何解决它。如果有人可以解释这是如何工作的,我将不胜感激。

谢谢 :)

这是播放器的设置方式: Player Inspector

PlayerControllerTopDown2D.cs

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

public class PlayerControllerTopDown2D : PhysicsObject2D
{
    public float maxSpeed = 7;

    private SpriteRenderer spriteRenderer;
    private Animator animator;

    private bool facingUp, facingDown, facingLeft, facingRight;

    void Awake()
    {
        spriteRenderer = GetComponent<SpriteRenderer>();
        animator = GetComponent<Animator>();

        facingUp = true;
        facingDown = facingLeft = facingRight = false;
    }

    protected override void ComputeVelocity()
    {
        Vector2 move = Vector2.zero;

        move.x = Input.GetAxis("Horizontal");
        move.y = Input.GetAxis("Vertical");

        targetVelocity = move * maxSpeed;

        if (move.y > minMoveDistance && !facingUp)
        {
            clearOthersAndSet(0);
            // sprite rotation
        }

        if (move.y < -minMoveDistance && !facingDown)
        {
            clearOthersAndSet(1);
            // sprite rotation
        }

        if (move.x < -minMoveDistance && !facingLeft)
        {
            clearOthersAndSet(2);
            // sprite rotation
        }

        if (move.x > minMoveDistance && !facingRight)
        {
            clearOthersAndSet(3);
            // sprite rotation
        }


    }

    void clearOthersAndSet(int x)
    {
        switch (x)
        {
            case 0;
                facingUp = true;
                facingDown = facingLeft = facingRight = false;
                break;
            case 1:
                facingDown = true;
                facingUp = facingLeft = facingRight = false;
                break;
            case 2:
                facingLeft = true;
                facingUp = facingDown = facingRight = false;
                break;
            case 3:
                facingRight = true;
                facingUp = facingDown = facingLeft = false;
                break;
        }
    }
}

PhysicsObject2D.cs

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

public class PhysicsObject2D : MonoBehaviour
{
    protected Rigidbody2D rb2d;
    protected Vector2 velocity;
    protected Vector2 targetVelocity;

    protected ContactFilter2D contactFilter;
    protected RaycastHit2D[] hitBuffer = new RaycastHit2D[16];
    protected List<RaycastHit2D> hitBufferList = new List<RaycastHit2D>(16);


    protected const float minMoveDistance = 0.001f;
    protected const float shellRadius = 0.01f;

    protected bool hitSomething = false;


    void OnEnable()
    {
        rb2d = GetComponent<Rigidbody2D>();
    }

    void Start()
    {
        contactFilter.useTriggers = false;
        int layerMask = Physics2D.GetLayerCollisionMask(gameObject.layer);
        contactFilter.SetLayerMask(layerMask);
        contactFilter.useLayerMask = true;
    }

    void Update()
    {
        targetVelocity = Vector2.zero;
        ComputeVelocity();
    }

    protected virtual void ComputeVelocity()
    {

    }

    void FixedUpdate()
    {
        if (hitSomething)
        {
            targetVelocity = -targetVelocity * 5;
            hitSomething = false;
        }

        velocity.x = targetVelocity.x;
        velocity.y = targetVelocity.y;

        Vector2 deltaPosition = velocity * Time.deltaTime;

        Vector2 move = Vector2.right * deltaPosition.x;

        Movement(move, false);

        move = Vector2.up * deltaPosition.y;

        Movement(move, true);
    }

    void Movement(Vector2 move, bool yMovement)
    {
        float distance = move.magnitude;

        if (distance > minMoveDistance)
        {
            int count = rb2d.Cast(move, contactFilter, hitBuffer, distance + shellRadius);

            if (count > 0)
                hitSomething = true;
            else
                hitSomething = false;

            hitBufferList.Clear();
            for (int i = 0; i < count; i++)
            {
                hitBufferList.Add(hitBuffer[i]);
            }

            for (int i = 0; i < hitBufferList.Count; i++)
            {
                float modifiedDistance = hitBufferList[i].distance - shellRadius;
                distance = modifiedDistance < distance ? modifiedDistance : distance;
            }
        }

        rb2d.position = rb2d.position + move.normalized * distance;
    }
}
4

3 回答 3

1

1) 对于像玩家这样的重要实体,将碰撞检测模式设置为“连续”。

2)rb2d.MovePosition();用于运动。

3)不要rb2d.MovePosition()在一个框架中多次调用。

这 3 个结合起来应该可以使您的运动和碰撞检测工作正常。我不会质疑您的其余代码,但这是我的一般性建议

Vector2 MovementDirection; //Assuming this is assigned in Update() from Input
public float MaxSpeed;
void FixedUpdate() 
{
    Vector2 finalMoveDir = MovementDirection.normalized * MaxSpeed;
    //
    //any additional changes to the final direction should happen here
    //
    finalMoveDir *= Time.deltaTime;
    rb2d.MovePosition((Vector2)transform.position + finalMoveDir);
}
于 2018-09-26T22:29:00.627 回答
1

简化,以同步的方式统一检查每一帧的碰撞(为了帧丢失补偿),如果您的对象快速移动(在短时间内覆盖很远的距离),您的对象有可能穿过墙壁那正是碰撞检查和另一个的时间间隔。以及在此线程上进行了说明和测试。

如果您的对象正在穿过一个对象,您首先要更改的是碰撞检测模式,当模式设置为离散时,您是说该对象正在以较低的速率检查碰撞。在此处输入图像描述

当您将其设置为连续时,对象会更频繁地检查碰撞。 在此处输入图像描述

所以可能将检测模式从“离散”设置为连续应该足以解决您的问题。

于 2018-09-26T21:50:09.990 回答
0

正如马修斯建议的那样,我将对撞机模式更改为连续,但问题仍然存在。我找到了一种方法来确保它发生。

为了完成这项工作,我删除了 PhysicsObject2D.cs 的第 38 行:

目标速度 = -目标速度 * 5;

看到这张图片

在这个位置,我按下左 + 下,玩家向上移动盒子碰撞器中。开始时玩家不在其他对撞机内。

当身体重叠时,它可以在内部自由移动,但移动速度要慢得多,方向相反,按向上移动向下,按向左箭头向右移动。

于 2018-09-26T22:06:53.447 回答