Search code examples
unity-game-engine

How do I calculate a Vector on the edge of a circle in Unity for a CharacterController?


In Unity, I need to move a CharacterController from v2 to v3. I have the following information: the centre point of the circle (v1); the current character position on the outside of the circle (v2); the angle (a). How do I calculate the move vector for the CharacterController so it moves from v2 to v3?

Finding v3


Solution

  • v3 = Quaternion.Euler(0, a, 0) * (v2 - v1) + v1
    motion = v3 - v2
    

    Explanations:

    • v2 - v1: Vector of v2 with v1 as the origin
    • Quaternion.Euler(0, a, 0) *: Rotate the above vector counter-clockwise by a degree around the y-axis. If this is about 2D, you need to rotate around the z-axis
    • + v1: Get the v3 position in world space
    • v3 - v2: The move vector is from v2 to v3