连接铰链关节时,将物体平稳移动到位置

本文关键字:移动 位置 连接 | 更新日期: 2023-09-27 18:07:49

我有一个球,当你点击屏幕时,它会从摆动的杆上释放出来。当抛出的球与另一个摇摆杆碰撞时,我在球上加了一个铰链接头,并将其连接到杆的末端。然而,负责碰撞的圆碰撞器并不在条的末端。所以,当我击中圆圈碰撞时,它将球附着在杆的末端,但它在适当的位置折断。我想让它看起来平滑地移动到适当的位置,而不是立即在适当的位置卡住。

我怎样才能把它平稳地移动到那个位置?

这是我用来在检测到碰撞时附加球的脚本:

using UnityEngine;
using System.Collections;
public class attachBall : MonoBehaviour {
public GameObject player;
public GameObject rope;
public HingeJoint2D hinge;
public Rigidbody2D rb;
public CircleCollider2D coll;
public GameObject cameraObject;
public float move;
public float x;
void OnTriggerEnter2D(Collider2D collider) {
    if (collider.tag == "rope") 
    {
        player = GameObject.FindGameObjectWithTag ("Player");
        player.AddComponent<HingeJoint2D> ();
        rope = collider.gameObject;
        hinge = player.GetComponent<HingeJoint2D>();
        rb = rope.GetComponent<Rigidbody2D>();
        hinge.connectedBody = rb;
        hinge.connectedAnchor = new Vector2(0,2.5f);
        rope.GetComponent<CircleCollider2D>().enabled = false;
        addPoint.scorePoint();
    }
}
void Update()
{
    x = this.gameObject.transform.position.x;
    cameraObject.transform.position = new Vector3 (x, 0, 0);
}
}

连接铰链关节时,将物体平稳移动到位置

有人在这里放了一个没有工作的答案,然后不久就把它删除了。然而,在那个回答中,我看到:

Vector3.MoveTowards

这激发了我的想象力,我想明白了。我的对象是用池创建的。在其中一个池对象上,我在栏的末尾添加了一个空对象。然后我得到了那个物体绕圈的位置,并使用Vector2.MoveTowards将我的球员(球)移向它。然后,我匹配了球的Vector2位置和空游戏对象,并说如果它们匹配,停止移动并连接我的铰链关节。

using UnityEngine;
using System.Collections;
public class attachBall : MonoBehaviour {
public GameObject player;
public GameObject endOfLine;
public Vector2 endOfLineCoords;
public Vector2 playerCoords;
public GameObject rope;
public HingeJoint2D hinge;
public Rigidbody2D rb;
public CircleCollider2D coll;
public GameObject cameraObject;
public float move;
public float x;
public static bool attach = false;
public static bool connected = false;
void OnTriggerEnter2D(Collider2D collider) {
    if (collider.tag == "rope") 
    {
        attach = true;
        rope = collider.gameObject;
        endOfLine = rope.transform.Find("endOfLine").gameObject;
        addPoint.scorePoint();
    }
}
void Update()
{
    if (attach == true && connected == false) {
        moveToPosition ();
    }
    moveCamera();
}
void moveCamera()
{
    x = this.gameObject.transform.position.x;
    cameraObject.transform.position = new Vector3 (x, 0, 0);
}
void moveToPosition()
{
    if (connected == false)
    {
        player = GameObject.FindGameObjectWithTag ("Player");
        //endOfLine = GameObject.FindGameObjectWithTag ("endOfLine");
        playerCoords = player.transform.position;
        endOfLineCoords = endOfLine.transform.position;
        player.transform.position = Vector2.MoveTowards (playerCoords, endOfLineCoords, .5f);
    } 
    if(playerCoords == endOfLineCoords)
    {
        connected = true;   
        player.AddComponent<HingeJoint2D> ();
        hinge = player.GetComponent<HingeJoint2D>();
        rb = rope.GetComponent<Rigidbody2D>();
        hinge.connectedBody = rb;
        hinge.connectedAnchor = new Vector2(0,2.5f);
        rope.GetComponent<CircleCollider2D>().enabled = false;
    }
}
}

我现在唯一的问题是,当我放慢球朝着空的游戏对象移动的速度时,它有时无法与它匹配,并且有点反弹试图抓住它。我想我可以通过设置一个if语句来连接铰链关节,当球接近物体VS处于相同的坐标时。

发给那个张贴临时答案的人。谢谢你=D