当你点击屏幕时,我有一个从摇摆杆释放的球。当被抛掷的球与另一个摆动杆碰撞时,我将一个铰链接头添加到球上并将其连接到杆的末端。然而,负责碰撞的圆形对撞机不在杆的末端。因此,当我碰到圆圈碰撞时,它将球附着在杆的末端但是它会卡入到位。我希望它看起来像是顺利地移动到位而不是立即卡入到位。
我怎样才能顺利地将其移动到那个位置?
这是我在检测到碰撞时用来附加球的脚本:
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);
}
}
答案 0 :(得分:1)
有人在这里放了一个不起作用的答案,然后很快将其删除。但是,在那个答案中我看到了:
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