简体   繁体   English

使用Lerp进行Unity 2D碰撞和移动

[英]Unity 2D Collisions and movement with Lerp

So I have the player with a Rigidbody2d and a box collider 2D with continuous collision detection and dynamic body type. 因此,我的播放器带有Rigidbody2d和具有连续碰撞检测功能和动态身体类型的盒子对撞机2D。 The boxes the player must jump on are dynamic until they have fallen when their rigidbody gets destroyed to stop the wobbling, the box also has a collider 2d on it yet still the player can move through it. 玩家必须跳动的盒子是动态的,直到刚体被破坏以停止摆动时它们掉落为止,盒子上还有对撞机2d,但玩家仍然可以在其中移动。 The player must move with a lerp as it must only move a certain distance. 播放器必须移动,因为它只能移动一定距离。 I'm pretty sure this is the problem, however, I don't know how t fix it. 我很确定这是问题所在,但是,我不知道如何解决。 I have multiple raycasts which work very well except when the player lands on the box diagonally. 我有多个光线投射,除了玩家对角地落在盒子上之外,它们的工作效果都很好。 Any help would be much appreciated.alt text 任何帮助将不胜感激。替代文字

Thanks 谢谢

    void update(){
    if (Input.GetAxis("Horizontal") > 0 && moveRight == true && moveRightTimer <= 0)
        {
            moveRightTimer = 0.15f;
            moveX += moveDistance;
            if(gameObject.transform.localScale.x < 0)
            {
                flipPlayer();
            }
        }
    }
    void FixedUpdate()
        {
            MovePlayer();
        }
    //Moves the player
    void MovePlayer()
    { 
        Vector2 PlayerPos = gameObject.transform.position;
        PlayerPos.x = moveX;

    GetComponent<Rigidbody2D>().MovePosition(PlayerPos + (Vector2)transform.position * Time.deltaTime);

}

在此处输入图片说明

When setting your player's position, use either Rigidbody2D.MovePosition() or Rigidbody2D.position to set the position of your transform, rather than the transform's position property directly. 设置播放器的位置时,请使用Rigidbody2D.MovePosition()Rigidbody2D.position设置变换的position ,而不是直接使用变换的position属性。 Rigidbody2D.MovePosition() accounts for the interpolation setting on your rigidbody if that is something you wish to use. 如果您要使用Rigidbody2D.MovePosition()来计算刚体上的插值设置。

Rigidbody2D.MovePosition 刚体2D.MovePosition

Rigidbody2D.position 刚体2D位置

Example: 例:

public Rigidbody2D rigid;
public bool UseMovePosition;

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

void FixedUpdate()
{
    if (UseMovePosition)
        rigid.MovePosition(transform.position + transform.forward * Time.deltaTime);
    else
        rigid.position = transform.position + transform.forward * Time.deltaTime;
}

These will set the position and adhere to any collisions that would occur. 这些将设置位置并坚持可能发生的任何碰撞。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM