- Home /
Need help rotating child of player based off of player movement.
I am trying to move a player with a sphere child representing the body. When I move the parent in one direction I want the sphere to react to that movement similar to how a RigidBody would move with force added. I am not using a RigidBody because there's other children of the player that I want to not be affected by the force added to a RigidBody. I have it working for when you move in one plane with this code.
if (moveDir.x > 0)
playerSphere.transform.Rotate(0, 0, -rotateSpeed * Time.deltaTime);
else if (moveDir.x < 0)
playerSphere.transform.Rotate(0, 0, rotateSpeed * Time.deltaTime);
if (moveDir.z > 0)
playerSphere.transform.Rotate(rotateSpeed * Time.deltaTime, 0, 0);
else if (moveDir.z < 0)
playerSphere.transform.Rotate(-rotateSpeed * Time.deltaTime, 0, 0);
but if I start moving in the X and Z planes or diagonally I start to run into problems where the rotation is thrown off because the transform of the child is changing directions. https://imgur.com/7vRofyU This is how the spin looks like right now. Any help would be appreciated.
Answer by tyruji · Jul 08, 2021 at 11:14 AM
You could use transform.RotateAround to rotate the ball around an axis that wont change (the parent). Example:
// I passed in the spheres position as the point to
// rotate around, so it won't change position,
// but only the rotation
playerSphere.transform.RotateAround( playerSphere.transform.position, -transform.forward * moveDir.x,
rotationSpeed * Time.deltaTime );
//and for the horizontal
playerSphere.transform.RotateAround( playerSphere.transform.position, transform.right * moveDir.z,
rotationSpeed * Time.deltaTime );
Good luck!
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Hi, knife hit, how do you control the speed of the wheel and the speed of the knife? 1 Answer
Player Movement & Blend Tree - Can anyone help with my script? 1 Answer
How to detect if player had switched direction of movement? 0 Answers
Will someone please help me??? 2 Answers