- Home /
How to move a child object freely (within), when parented to a new parent Object?
I am creating a moving platform a player can stand on, the platform is simple, just moves left and right. so what i have done is simply move the player Object and attach it to the platform Object whenever OntriggerEnter2D, and on Exit it just simply gets removed as a child of the platform, simple enough.
The problem, however, is when once player becomes the child of the platform it is nearly imposible to move, i can move just incredibly slow (i have the platform set to trigger and rigid to kinematic) where have i gone wrong and why cant i move normally? Code below if needed, any help is greatly appreciated.
//This is isometric Player movement strait from Unity sources
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);
}
//this is the moving Platform movement
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);
}
}
//and this is the interaction is being handled
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;
}
}
}
Answer by WIIASD · Aug 10, 2020 at 02:39 AM
You are actually using Moveposition of rigidbody to move the player, but while on the platform, the player's position is already continuously changing even without input so that when you get your player moving in the opposite direction of the platform, I believe their position changing will be partially cancel out.
The solution I can come up with is to simply modify the velocity of the rigidbody to move it instead of changing its direction directly
Answer by YosephWordofa · Aug 10, 2020 at 11:28 AM
this should do it figured it out no to long ago, could have used your feedback before i lost my mind lol!
``rbody.velocity = new Vector2(horizontalInput, verticalInput);
Your answer
Follow this Question
Related Questions
Player slides off from moving platform when player isn't moving Bolt/visual scripting 0 Answers
Player falling through the floor on running? 1 Answer
Rigidbody stops working when parented to a moving object 0 Answers
How to realize a moving, rotating Spaceship with a moving player with top down view ? 2D 0 Answers
Player movement on tilting platform 0 Answers