Hi guys, I have an issue with my small prototype.
My ingame character (a spaceship) orbits around a planet using transform.RotateAround function.
I want the ship to be aligned with the tangent of the orbit.
My idea was to use the circle equations and calculate the position of a new point on the tangent and then use transform.LookAt(point) to rotate the ship, but the results are wrong so I need a different approach.
Any suggestions about how to align the ship with the tangent or give the illusion that the ship is aligned with the tangent of the circle are highly appreciated.
You can do this easy with 2 GameObjects. One object that is the point that you want to orbit around. Add the ship as a child of that object and position it as you would like to have it orbit. Then all you need to do is rotate the first object and the second will follow.
Thank you Chris for your answer! I wasn’t clear enough in my first post. My ship can reach the orbit from different angles, and even if I make it a children of the planet to make it rotate with the planet, I don’t know how much to rotate it around its Z to have it aligned with the tangent at the circle.
I usually play around with rotation in the editor to get the right rotations and then use those in a script. There is probably a better way, I haven’t tried anything like this before so I’m not sure what to do exactly. Maybe someone else who has done this before can help?
I know this post is old as shit, but I was trying to do the same thing as Negret, and I figured a nice solution out. Essentially you’ll use the cross product to get the vector you’re looking for. I’m not sure if it’s quite the tangent, but it’s close enough to be convincing. Here’s some code. Note that this code assumes the axis of movement is (x,z) and z is up. If you’re using this in a 2D scene you’ll have to adjust your vectors a little since the axis of movement is (x,y) z is forward.
// Get the vector going from the rotating object to the planet
Vector3 lookAtVector= rotatingObject.transform.position - rotationTarget.transform.position;
// Cross product of that Vector and transform.up will give us a vector that looks close to the tangent of the rotation
shootVectorDir = Vector3.Cross(lookAtVector, transform.up).normalized;
Example in the game I’m working on:
the cyan Vector is the result.
Note that the hitbox is not necessarily the rotation path, but it gives a general idea. Also the cyan vector here was multiplied by 5 to give a better visual. If you’re confused at all about the cross product, check out this resource. x.com