【问题标题】:Move object to a certain position with speed Unity (2D)以 Unity (2D) 的速度将对象移动到某个位置
【发布时间】:2021-09-06 22:40:48
【问题描述】:

我有 2 个移动物体,它们在一个方向上基本运动:

if(CircleMovement1.switchMovement1 == false)
        {
            if(scoreNumber.scoreCounter >= 0 && scoreNumber.scoreCounter < 10){
                speed = 0.3f;
                rb.velocity = new Vector2(speed * 1.7f, 0);
            }
}

成功时,即点击类型的事件,对象必须停止沿该方向移动并返回到相反方向的起始位置,其速度分量比它们在开始方向上移动时的速度分量要大一些。在单击事件时,只需更改“CircleMovement1.switchMovement1”的布尔值,以便对象可以向相反方向移动:

else if(CircleMovement1.switchMovement1 == true)
        {
            if(scoreNumber.scoreCounter >= 0 && scoreNumber.scoreCounter < 10){
                rb.velocity = new Vector2(-speed * 25f, 0);
            }}

问题是:一旦对象再次到达起始位置,它们必须停止并再次向前移动,就像 CircleMovement1.switchMovement1 == false 一样,如第一个 sn-p 所示。我不知道如何标记他们到达起点并可以开始向相反方向移动的时刻。我尝试添加碰撞器并检测与起点上的对象的碰撞:

public void OnTriggerEnter2D(Collider2D col){
    if (col.gameObject.tag == "mainCol")
    {
        CircleMovement1.switchMovement1 = false;
    }
}

但这并没有很好地转向,因为在更高的速度上,物体有时会穿透起点的对撞机并向后移动超过需要的程度(这是不一致的)。

我尝试像这样跟踪对象的本地位置:

if(this.GetComponent<RectTransform>().localPosition.x == 30) {
        CircleMovement1.switchMovement1 = false;
    }

但这根本不起作用。

最后,我尝试使用 Lerp 函数将对象移动到这样的起始位置:

circ3.GetComponent<RectTransform>().localPosition = new Vector3(Mathf.Lerp(currPos3.x,-1,1f * Time.deltaTime),2,0);

这可行,但不是我想要的方式。它只是立即在给定的矢量位置生成一个对象,并且与速度运动的外观不同。

如果有人知道如何检测物体到达他们需要的位置的点,请随时提供帮助。

提前致谢。

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    您已显示此代码。 x 位置很少或永远不会恰好达到 30,因为它是如何移动的并且它具有十进制数字。这就是它不起作用的原因。

    if(this.GetComponent<RectTransform>().localPosition.x == 30) {
            CircleMovement1.switchMovement1 = false;
    }
    

    您可以尝试检查它到目标位置的距离,如下所示:

    float distance = Vector3.Distance(GetComponent<RectTransform>().localPosition, targetPos);
    if (distance < 1)
    {
        CircleMovement1.switchMovement1 = false;
    }
    

    【讨论】:

    • 谢谢,我已经试过了,但是很不一致,有时太长有时太短。
    • 在您的问题中,您正在谈论快速的速度。我们谈论的速度有多快?
    • rb.velocity = new Vector2(0.3 * 1.7f, 0) -> 这不是很快 rb.velocity = new Vector2(3.7 * 1.7f, 0) -> 这非常快 它不是就像难以置信的快,但如果我使用距离比较方法和对撞机,脚本无法及时做出反应就足够了。我想我必须找到正确的方法来使用 lerp 或 smthn。
    • 我们所说的边距是多少?当我使用上面的代码进行测试时,对象距离它的位置大约 0.2 个单位。您始终可以在到达后手动将其位置设置到所需的点,以确保它处于您想要的精确坐标中。
    【解决方案2】:

    我通过正确使用 Lerp 函数设法解决了这个问题,我认为这是实现一致性和准确性的唯一方法:

     void Update()
    {
        if(callLerp==true){
            if(Vector3.Distance(circ1.GetComponent<RectTransform>().localPosition, circ2.GetComponent<RectTransform>().localPosition) > 3){
                  circ1.GetComponent<RectTransform>().localPosition = Vector3.Lerp(circ1.GetComponent<RectTransform>().localPosition, new Vector3(-1,2,0), Time.deltaTime * 10f);
        circ2.GetComponent<RectTransform>().localPosition = Vector3.Lerp(circ2.GetComponent<RectTransform>().localPosition, new Vector3(30,2,0), Time.deltaTime * 10f);
            }
            else{
                callLerp = false;
                CircleMovement1.switchMovement1 = false;
            } 
            
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-12-25
      • 2018-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多