Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Game object looking along the tangent of a circle

Discussion in 'Scripting' started by Negrat, Aug 1, 2015.

  1. Negrat

    Negrat

    Joined:
    Oct 1, 2013
    Posts:
    11
    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.
     

    Attached Files:

  2. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,260
    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.
     
  3. Negrat

    Negrat

    Joined:
    Oct 1, 2013
    Posts:
    11
    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.
     
  4. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,260
    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?
     
  5. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Surely, you just rotate 90 degrees from looking at the center
    that's like, the very definition of the tangent angle
     
  6. samsdevlog

    samsdevlog

    Joined:
    Nov 21, 2020
    Posts:
    1
    I know this post is old as S***, 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.


    Code (CSharp):
    1. // Get the vector going from the rotating object to the planet
    2. Vector3 lookAtVector= rotatingObject.transform.position - rotationTarget.transform.position;
    3. // Cross product of that Vector and transform.up will give us a vector that looks close to the tangent of the rotation
    4. shootVectorDir = Vector3.Cross(lookAtVector, transform.up).normalized;
    Example in the game I'm working on:
    the cyan Vector is the result.
    upload_2023-1-14_8-59-49.png
    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. https://twitter.com/freyaholmer/status/1203059678705602562?lang=en