- Home /
Problem with setting transform.up away from object
I'm making a game where the player can walk around a "planet" in 2D by setting transform.up to the vector from the player to the centre of the planet. This works for the most part, but when the player is facing downwards it gets stuck and starts to rotate wierdly. Is the problem that i should be using Quaternions or is it something else? Does anyone have an explanation on how in that case?
PlanetVector = ((Planet.transform.position - transform.position).normalized) * -1; transform.up = PlanetVector;
Answer by Bunny83 · Nov 20, 2020 at 05:18 PM
Well, a common solution is to just use FromToRotation and specify the current up vector as "fromDirection" and your target up vector as "toDirection". This will result in a relative rotation that you should apply to the current orientation.
var rotateUpwards = Quaternion.FromToRotation(transform.up, PlanetVector);
transform.rotation = rotateUpwards * transform.rotation;
Your answer

Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Make movement of an object independant. 1 Answer
Unity C# 2D Adding Velocity on rotation 1 Answer
Multiple Cars not working 1 Answer
Flip Sprite C# 0 Answers