- Home /
How to move child with parent within the same frame. (Before trigger events)
I have 2 game objects - the player, a moving platform. The platform has 2 box colliders; one a trigger, the other not. The player has a capsule collider and a character controller. When the player lands on top of the platform, it enters the trigger and makes the platform its parent so that the platform can move the player with it. This works perfectly until the platform moves downward on the Y axis. What I believe is occurring is the following:
Platform moves down
Trigger Exit activated
Platform removed as player parent
Gravity applies to player
Player enters trigger
Repeat
This causes the player object to constantly bounce the entire way down. Is there a way to make the child (player) move with its parent (platform) within the same frame, or before trigger events occur?
My code for reference:
/////////////////////////////
//within player object
/////////////////////////////
void OnTriggerStay(Collider other)
{
if(other.gameObject.tag == "Platform")
{
transform.parent = other.gameObject.transform;
}
}
private void OnTriggerExit(Collider other)
{
if(other.gameObject.tag == "Platform")
{
transform.parent = null;
}
}
////////////////////////
// Within platform
////////////////////////
void Update () {
if (!isResting)
{
if (xAxis)
{
transform.position += new Vector3((speed * (xInvert ? -1 : 1)) * Time.deltaTime, 0, 0);
}
if (yAxis)
{
transform.position += new Vector3(0, (speed * (yInvert ? -1 : 1)) * Time.deltaTime, 0);
}
if (zAxis)
{
transform.position += new Vector3(0, 0, (speed * (zInvert ? -1 : 1)) * Time.deltaTime);
}
}
elapsedTime += Time.deltaTime;
if(elapsedTime >= travelTime && !isResting)
{
elapsedTime = 0f;
xInvert = !xInvert;
yInvert = !yInvert;
zInvert = !zInvert;
if(restTime > 0f)
{
isResting = true;
}
}
if(isResting)
{
if(elapsedTime >= restTime)
{
elapsedTime = 0f;
isResting = false;
}
}
}
Answer by uulamock_unity · Sep 29, 2017 at 09:34 PM
According to the execution order (image below), OnTrigger events get called before Update events. If this is true, how is it possible then for a parent to move during update and not have the child move with it (assuming the child has no movement) at the same time, before an OnTriggerExit() can occur?
Things I've tried:
Make player stationary (won't move until its platform's child)
putting player on LateUpdate so as to move after the updated platform
Increasing trigger size (which slightly works, but causes very awkward movement)
The issues is the player object leaves the trigger zone [only] when the platform moves down, causing a bouncing effect the whole way down. The only reason this can happen is if the platform moves without the player, causing a TriggerExit. How can this be avoided?
Answer by tormentoarmagedoom · Sep 28, 2017 at 08:33 AM
Good day @uulamock_unity !!
I did not read your code, i have no time :D I recommend you to read this manual showing order of executions , to decide when/where to execute your commands to prevent this kind of problems
If helped, Accept the answer as correct!!
Bye :D :D
This is extremely useful, but sadly does not give me the answer I need. The issue is the need to have a child move with its parent before a TriggerExit can be called, however, this shows that trigger events are called before update events, which begs the question, "why does the child not move with the parent before TriggerExit can be called?"
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
ENEMY MOVEMENT SCRIPT 0 Answers
Deactivate/Activate FPS Movement 0 Answers