简体   繁体   English

Unity2D:ForceMode2D.Impulse将播放器拖回

[英]Unity2D: ForceMode2D.Impulse dragging player back

I have a script attached to a tagged object that pushes my player on trigger. 我将脚本附加到带标签的对象上,该脚本可将播放器推入触发器。 It works perfectly, however my player (after collision) gets dragged back to the tagged object and continues bouncing. 它可以完美工作,但是我的播放器(在碰撞后)被拖回到标记的对象上并继续弹跳。 How would I stop this. 我将如何阻止这个。 All I want is for my player to get pushed back by the tagged object for a short amount of distance and stay there. 我想要的只是让我的播放器被标记的对象后退一小段距离并停留在那里。 Could anyone help me with this? 有人可以帮我吗?

This is my script: 这是我的脚本:

void OnTriggerEnter2D(Collider2D other) 
{
    if (other.tag == "Bouncy object")
        GetComponent<Rigidbody2D>().AddForce(transform.right * 15, ForceMode2D.Impulse);
}

Try using a vector in your AddForce() method rather than affecting the transform directly: 尝试在您的AddForce()方法中使用向量,而不是直接影响转换:

void OnTriggerEnter2D(Collider2D other) 
{
    if (other.tag == "Bouncy object")
        GetComponent<Rigidbody2D>().AddForce(new Vector2(15, 0), ForceMode2D.Impulse);
}

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

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