当成为新父对象的父对象时,如何自由地(在内部)移动子对象?
How to move a child object freely (within), when parented to a new parent Object?
我正在创建一个玩家可以站立的移动平台,平台很简单,只是左右移动。
所以我所做的只是简单地移动播放器对象并将其附加到平台对象,每当 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;
}
}
}
如果有一天其他人遇到这个问题,这是我自己解决问题的方法...问题出在球员运动中,刚性是一个大问题,因为它被部分抵消了成为其他移动object的child。所以就这样做吧...
rbody.velocity = new Vector2(horizontalInput, verticalInput);
我正在创建一个玩家可以站立的移动平台,平台很简单,只是左右移动。 所以我所做的只是简单地移动播放器对象并将其附加到平台对象,每当 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;
}
}
}
如果有一天其他人遇到这个问题,这是我自己解决问题的方法...问题出在球员运动中,刚性是一个大问题,因为它被部分抵消了成为其他移动object的child。所以就这样做吧...
rbody.velocity = new Vector2(horizontalInput, verticalInput);