- Home /
How can I make a First Person Character Controller a child of a moving platform without it affecting the rotation of the Character Controller.
So, I have an array of four square platforms that rotate in a circle around a center point. They each pass by the square that the player spawns on. When the player jumps onto one of the platforms to ride it around the loop, the Character Controller becomes a child of the platform, but that means that when the player jumps onto the platform, their rotation gets switched to the platform's current rotation. I'd like the player to be able to move freely while the platform carries them around, but I can't figure out how to prevent the rotation issue and move the player with the platform without essentially gluing their feet to the it. Are there any ways to allow a player to ride the platforms while still being completely in control of their rotation? The code I'm currently using is below. The platforms are parented by a null object so that they can rotate as a single entity.
Answer by GreenSerpent · Nov 26, 2018 at 02:28 AM
Maybe you can try adding how much the platform moved each frame to the player's position. This would require that they aren't parented to each other. I'll write your script so you can try this.
public GameObject player;
public Vector3 lastPos = Vector3.zero;
public Vector3 changeInPos = Vector3.zero;
public bool playerAttached = false;
void LateUpdate () {
changeInPos = transform.position - lastPos;
lastPos = transform.position;
if (playerAttached) {
player.transform.position += changeInPos;
}
}
void OnTriggerEnter () {
if (other.gameObject == player) {
playerAttached = true;
}
}
void OnTriggerExit () {
if (other.gameObject == player) {
playerAttached = false;
}
}
It worked perfectly. Thank you for your help. I'm new to Unity/C# program$$anonymous$$g so it's really cool that there's such a supportive community.
Great to know that it worked! Even as someone who has been using Unity for years, I look at posts constantly to help me out. You're right, the community is awesome.
Your answer
Follow this Question
Related Questions
Behave like another object is parent? 2 Answers
Camera follow ball along cylinder 1 Answer
Make a simple tree 1 Answer
Where to find original unity Parent/Child script? 2 Answers
How can I identify a non-uniform scaled mesh, and fix it? 1 Answer