Search code examples
c#unity-game-engineunity3d-2dtools

Moving objects with angle and units in unity


Hi I am learning unity/c# and I am stuck at a point please help...

I want a object like cube or sphere in 2 D environment to move to a particular spot without the use of x-axis and y-axis... I want o specify the angle and units it should move. Like Move 4 units in 45 degree angle.

Thankyou alot


Solution

  • First you need to specify how many is 1 unit in your environment and then you can use trigonometrics to calculate your point.

    and make a function like

    public Vector2 GetCoordinates(float units, float angle){
      //Your awesome trigonometics code here!
    }
    

    Note: A is your current position,B is the final position, and c is your units' and the angle cb is your angle.

    b is your movement on x coordinate, and a is your movement in the y coordinate

    enter image description here

    Happy coding! (if you need further explanation i can do it but try to figure out first :D)