- Home /
Creating a function that parents player to object and then unparents it
I have a game where the player can rotate a level around them by pressing a button. but when I rotate the level a certain way the level heavily pushes the player; so much so that at times the player gets flung so fast it ignores any colliders I set up. I'm under the impression that parenting the object to the level while it's rotating and then un-parenting upon completion should solve this problem; but I haven't a clue how to go about doing that without using triggers. Could somebody help?
Added code I have so far
function RotateObjectForward(point : Vector3, axis : Vector3,rotateAmount : float, rotateTime : float) { var rotation = Quaternion.AngleAxis(rotateAmount, axis); var startPos : Vector3 = transform.position - point; var endPos : Vector3 = rotation * startPos; var startRot : Quaternion = transform.rotation; var step : float = 0.0; //non-smoothed var smoothStep : float = 0.0; //smoothed var rate : float = 1.0/rotateTime; //amount to increase non-smooth step by Player.transform.parent = Parent.transform; while(step < 1.0) { // until we're done step += Time.deltaTime * rate; //increase the step smoothStep = Mathf.SmoothStep(0.0, 1.0, step); transform.position = point + Vector3.Slerp(startPos, endPos, smoothStep); transform.rotation = startRot * Quaternion.Slerp(Quaternion.identity, rotation, smoothStep); yield; } //finish any left-over if(step > 1.0) { transform.position = point + endPos; transform.rotation = startRot * rotation; } Player.transform.parent = null;
}
I get this weird error that says "Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption."
Answer by Eric5h5 · Apr 18, 2011 at 02:20 AM
Set the transform of the instance of the prefab, instead of the actual prefab.
Use the instance of the prefab that's instantiated into the scene. Not the actual prefab.
I think I got it work, but I guess it wasn't what I was lookin for. At least I understand how to do it now, Thanks a bunch!
Your answer
Follow this Question
Related Questions
How can I stop a parented object from resetting its transform when a parent animation play? 1 Answer
Children ignoring Parent control. [Solved] 1 Answer
Trying to understand parenting 1 Answer
X-Flip is making my rotation break? 2 Answers
how to make an objects rotation independent from its parents. 2 Answers