- Home /
[Please!] Noob need's help with planet gravity movement.
So I have a capsule that uses custom gravity to walk around a sphere, I have used "camera.ScreenPointToRay" to get the point on the planet where I click and then "transform.movetowards" to have him move towards that point,
My problem is I need to have him look at the point he is moving towards but to ONLY ROTATE AROUND HIS LOCAL Y AXIS to look at it. This is confusing me as the capsule could be at any rotation given where he is located on the sphere.
I have added 2 gifs, this first one is how the player moves around the sphere. https://i.imgur.com/N9kqmUy.gifv
This second one shows how I want the capsule to look in the direction he is moving but I want him rotating ONLY AROUND Y AXIS. https://i.imgur.com/7UoAVhZ.gifv
Thankyou so much for your help! This has been giving me a headache for 3 days now!
Answer by osrs320 · Aug 03, 2018 at 05:59 AM
Figured out a way to do it, created a plane as a child on my capsule/player, flipped it upside down so that the first ray from screen to mouse goes through it, then I cast a second ray from the point on the ground in a transform.up direction which hits the plane at the same height at the player. I then look at that new vector for only one frame, and implement the step eno-Khaon suggested in the look at function. Everything works perfectly now I'm so happy lol XD
Answer by madks13 · Jul 30, 2018 at 08:38 AM
The simplest solution would be to use LookAt, then restore the x and z axis :
var rotation = go.transform.rotation.eulerAngles;
go.transform.LookAt(clickedPoint);
go.transform.eulerAngles = new Vector3(rotation.x, go.transform.eulerAngles.y, rotation.z);
Or something like that. This is untested and unchecked but you get the idea.
It looked like the general rotation was working and the clicked point is clearly defined, so it should be possible to simply enforce a new "up" for the second argument in Transform.LookAt(). In fact, if the local rotation is handled after movement for the current frame, you already have your new "up" available:
// C#
Vector3 currentUp = transform.up;
transform.LookAt(clickedPoint, currentUp);
It's the closest i've gotten but still doesn't work, unfortunately as it says in the documentation for transform.LookAt "worldUp is only a hint vector. The up vector of the rotation will only match the worldUp vector if the forward direction is perpendicular to worldUp." :(
If i could I could get the ".point" of my "RayCastHit" in "space.self" regardless of my rotation in world space that might work... but i'm not sure that's how "transform.InverseTransformPoint" works... i've messed around with it and i couldn't work it out.
Your answer
Follow this Question
Related Questions
Local child rotation and transform.LookAt 2 Answers
Finding the Euler angles of an object's rotation in terms of a different set of axes 0 Answers
Rotate with joystick - LookAt axis flipping 1 Answer
how can i substract one axis by 180? 2 Answers
Quaternion Equivalent of restricting an axes rotation to zero 3 Answers