gpt4 book ai didi

c# - 连接铰链接头时将物体平稳地移动到位

转载 作者:太空宇宙 更新时间:2023-11-03 13:02:20 25 4
gpt4 key购买 nike

我有一个球,当您点击屏幕时,它会从摇杆上释放出来。当抛出的球与另一个摆动杆碰撞时,我在球上添加一个铰链接头并将其连接到杆的末端。但是,负责碰撞的圆形碰撞器并不在栏的尽头。所以,当我击中圆圈碰撞时,它会将球附着在杆的末端,但它会卡在适当的位置。我希望它看起来像是平稳地移动到位,而不是立即卡入到位。

我怎样才能顺利地将它移动到那个位置?

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

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

关于c# - 连接铰链接头时将物体平稳地移动到位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31971102/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com