- Home /
How to get new position of object based on start point, distance and angle?
How to get position based on start point, distance and angle?
Here is my implementation:
Vector3 GetObjectPosition(Vector3 startPoint, float distance, float angle)
{
Vector3 newPos = new Vector3 (startPoint.x, startPoint.y);
handObject.transform.Rotate (newPos, angle);
newPos = handObject.transform.forward * distance;
return newPos;
}
void Update ()
{
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
mousePos.z = 0;
float angle = Vector2.Angle(this.transform.position, mousePos);
Vector3 handPos = UpdateHandPosition (this.transform.position, 10.0f, angle);
handObject.transform.position = handPos;
print (handPos);
}
As result it blinks(moves very fast) and doesn't work properly
Your missing a direction. You have a point but you need a direction or an initial rotation. Else, where are you heading for your distance?
Once you have direction then, take the point, extend it by distance in the direction, then rotate with Quaternion.AngleAxis or Quaternion.Euler.
thank you for reply. please take a look to my updated topic. the start point is game object (player) and end point is mouse position
Answer by Divinyx · Jan 14, 2018 at 11:24 PM
you can use SOH CAH TOA to get a direction vector, multiply it by distance then add it to startPoint
so your direction vector is something like Vector2(x = Mathf.Cos(angle), y = Mathf.Sin(angle))
convert your answer to degrees or keep in radians depending on how your angle is used
Answer by black_sheep · Jan 14, 2018 at 11:54 PM
Vector2 GetObjectPosition(Vector2 startPoint, float distance, float angle){
transform point; //i don't remember how to rotate a vector2 so i used a transform
point.position = startPoint; //i'm sure there's a function to rotate a vector2
point.rotate(angle); //now it's pointing to the desired new location
point.forward * distance; //now walk forward that much of a distance
//transform.forward is shorthand for (1,0,0). If your distance is 1.5f, it'll be(1.5f, 0, 0)
return transform.point;
}
thank you for response. I've made how you describe but doesn't work. Please check updated topic
Your answer
![](https://koobas.hobune.stream/wayback/20220612150511im_/https://answers.unity.com/themes/thub/images/avi.jpg)