- Home /
Rotate object toward waypoint on moving object
I am currently creating a RTS game where the map is a custom mesh mapped to the shape of a sphere. I then have a faux gravity towards the planet that holds the units to the planet. I am currently working on the pathfinding for the units and right now I'm just trying to get the units to a waypoint (the location of a right click). The units are children on the planet (for reasons explained at the end)
Right now I have:
Vector3 normal = GetComponent<Attract>().terrain normal;
Vector3 direction = waypoint - transform.position;
transform.rotation = Quaternion.LookRotation(Vector3.ProjectOnPlane(direction, normal), normal);
Waypoint is defined from a raycast that hits the planet.
My problem now is the planet the unit is on is both spinning in space and rotating around a sun. This is why I have made all the units children of the planet. This spinning also means that the definition of the waypoint drifts off the surface of the planet and causes the unit to walk in weird circles.
basically, I'm trying to figure out how to define the waypoint as a child of the planet's position, and then have the unit rotate towards that point (not concerned about slerp or anything). What I have now currently moves it in the right direction if the planet is not moving.
If anyone needs clarification or a greater breakdown of my code please let me know.
Answer by FortisVenaliter · Dec 14, 2017 at 06:45 PM
Well, if you're using local space, why are you using global-space variables?
You should be using localPosition instead of position, and localRotation instead of rotation.
I feel stupid for not thinking to implement localRotation. It probably would fix the issue, and I will definitely keep it in $$anonymous$$d for the rest of the project, however, I came up with a solution that fixes this issue. I will definitely be reviewing what I have to improve it with localPosition and localRotation. Thanks for the help.
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
How can I add a rotation to my bullets when they are shot? 1 Answer
How to change Vector3 if an objects rotation is a certain number? 1 Answer
How do i get the Raycast rotation? 1 Answer
How to change the player's forward direction and make turning more smooth 0 Answers