Unity2D推拉不同形状的物体

时间:2018-06-16 23:06:22

标签: c# unity3d

你好我的剧本有麻烦,玩家只能推出一个完美的方形物体。我添加了L形this one的物体,我试图通过获得对撞机的侧面来增加拉力,但它不起作用 该对象有2个boxcolliders。因为当我的代码基于玩家位置和可推动物体的大小时,当玩家将其推向L的中心时它是一个Lshape,因为它是一个Lshape

这是我的可推送对象的代码

public class PushableObject : MonoBehaviour {

[SerializeField] private Player player;
[SerializeField] private float speed = 2;
[SerializeField] private float distanceX = 2;
[SerializeField] private float distanceY = 2;
[SerializeField] List<PushableObject> objectsInArea = new List<PushableObject>();

private bool isPlayerColliding = false;
private bool isMoving = false;


public bool IsMoving
{
    get { return isMoving; }
}

private void FixedUpdate()
{
    if (isPlayerColliding && player.IsInteractingObject && isAnyOtherObjectMoving())
    {
        move();
        isMoving = true;
    }
    else if(Input.GetKeyUp(KeyCode.E))
        isMoving = false;
}

private void move()
{
    Vector2 direction = transform.position - player.transform.position;

    float x = Mathf.Abs(direction.x), y = Mathf.Abs(direction.y);

    direction.x = x > y ? direction.x : 0;
    direction.y = y > x ? direction.y : 0;

    Vector2 targetPos = new Vector2(transform.position.x, transform.position.y) + direction;

    transform.position = Vector2.MoveTowards(transform.position, targetPos, speed * Time.deltaTime);
}

private bool isAnyOtherObjectMoving()
{
    foreach(PushableObject po in objectsInArea)
    {
        if (po.IsMoving)
            return false;
    }

    return true;
}

private void OnCollisionEnter2D(Collision2D collision)
{
    if(collision.gameObject.CompareTag("Player"))
    {
        isPlayerColliding = true;
    }

}

private void OnCollisionExit2D(Collision2D collision)
{
    if (collision.gameObject.CompareTag("Player"))
    {
        isPlayerColliding = false;
    }
}

}

0 个答案:

没有答案