gpt4 book ai didi

unity3d - 当成为新父对象的父对象时,如何自由地(在内部)移动子对象?

转载 作者:行者123 更新时间:2023-12-05 07:03:07 25 4
gpt4 key购买 nike

我正在创建一个玩家可以站立的移动平台,该平台很简单,只需左右移动即可。所以我所做的只是简单地移动播放器对象并将其附加到平台对象,每当 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;
}

}
}

最佳答案

如果有一天其他人遇到这个问题,这是我自己解决我遇到的问题的方法...问题出在球员运动中,刚性是一个大问题,因为它在成为其他移动物体的 child 。所以就这样做吧......

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

关于unity3d - 当成为新父对象的父对象时,如何自由地(在内部)移动子对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63324583/

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