当作为新父对象的父对象时,如何自由移动子对象(在)



我正在创建一个玩家可以站在上面的移动平台,这个平台很简单,只需左右移动。所以我所做的只是简单地移动播放器对象,并在OntriggerEnter2D时将其附加到平台对象上,在退出时,它只是作为平台的子对象被移除,这很简单。

然而,问题是,当玩家成为平台的孩子时,几乎不可能移动,我可以移动得非常慢(我将平台设置为触发器,并将其刚性设置为运动学(,我哪里出了问题,为什么我不能正常移动?下面的代码如果需要,任何帮助都将不胜感激。

//这是来自Unity来源的等距玩家移动海峡

void FixedUpdate()
{
Vector2 currentPos = rbody.position;
float horizontalInput = Input.GetAxis("Horizontal"); //* movementSpeed;
float verticalInput = Input.GetAxis("Vertical"); // * movementSpeed;
Vector2 inputVector = new Vector2(horizontalInput, verticalInput);
inputVector = Vector2.ClampMagnitude(inputVector, 1);
Vector2 movement = inputVector * movementSpeed;
Vector2 newPos = currentPos + movement * Time.fixedDeltaTime;
isoRenderer.SetDirection(movement);
rbody.MovePosition(newPos);
}

//这是移动平台移动

public class Patroll: StateMachineBehaviour {
GameObject NPC;
GameObject[] waypoints;
int currentWP;
void Awake() {
waypoints = GameObject.FindGameObjectsWithTag("WayPoint");
}
override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
NPC = animator.gameObject;
currentWP = 0;
}
override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
if (waypoints.Length == 0) return;
if (Vector2.Distance(waypoints[currentWP].transform.position, NPC.transform.position) < 0.2f) {
currentWP++;
if (currentWP >= waypoints.Length) {
currentWP = 0;
}
}
NPC.transform.localPosition = Vector2.MoveTowards(NPC.transform.position, waypoints[currentWP].transform.position, Time.deltaTime * 1.0f);
}
}

//这就是正在处理的交互

public class MovingObject: MonoBehaviour
{
private void OnTriggerEnter2D(Collider2D other) {
if (other.transform.gameObject.tag == "Player") {
other.transform.parent = transform;
//other.transform.parent.SetParent(transform);
}
}

private void OnTriggerExit2D(Collider2D other) {
if (other.transform.gameObject.tag == "Player")
{
other.transform.parent = null;
}
}
}

如果有一天其他人遇到这个问题,下面是我自己解决这个问题的方法。。。问题出在玩家的移动上,刚性是一个大问题,因为它变成了其他移动物体的孩子,被部分抵消了。所以就这样吧。。。

rbody.velocity = new Vector2(horizontalInput, verticalInput);