Get coordinate with angle and distance
Hello,
Basically I have a base in the center of the screen, and I want enemies to spawn at random angle from player, offscreen then fly towards the base.
The trouble I have is whats the best way to get coordinate that is X distance away, at Z angle? Something like draw a stick of 5 meters at 37 degrees and get the coordinate of the end point.
I sort of know you can do this with casting ray at angle and getting end point, but I was wondering if there is less costly way to do this?
thanks
If you learn quaternion math this will find a point 7 away from you at 35 degrees on Y: transform.position + Quaternion.Euler(0,35,0) * (Vector3.forward*7);
.
Even if you're good at trig, quaternions are easier for almost everything.
Answer by rutter · Jul 29, 2014 at 11:25 PM
If you had a decent math teacher, they'll have shown you that what we call "trigonometry" is also useful for studying circles. In particular, the famous sine and cosine functions actually give you points on a circle.
Unity's Mathf class has both sin and cos functions, but they use radians instead of degrees. What are radians? Well, they're a lot like degrees.
//convert from degrees to radians
var radians = degrees * Mathf.Deg2Rad;
So, let's say you want a point that's 36 degrees around a circle.
var degrees = 36;
var radians = degrees * Mathf.Deg2Rad;
var x = Mathf.Cos(radians);
var y = Mathf.Sin(radians);
var pos = Vector3(x, y, 0); //Vector2 is fine, if you're in 2D
Now, that gives us a point around a "unit circle", which has a radius equal to exactly one. You want a radius of five? Just multiply!
var radius = 5;
pos = pos * radius;
Thanks it works, this is what I ended up with
Vector3 spawnPoint;
int degree = Random.Range(0,360);
float radian = degree*$$anonymous$$athf.Deg2Rad;
float x = $$anonymous$$athf.Cos(radian);
float y = $$anonymous$$athf.Sin(radian);
spawnPoint = new Vector3 (x,y,0)*spawnDistance;
spawnPoint = game$$anonymous$$anager.planet.transform.position + spawnPoint;
return spawnPoint;
If you want a smooth distributed angle either change the Random.Range to use float values (and the degree variable as well), or completely replace those lines:
int degree = Random.Range(0,360);
float radian = degree*$$anonymous$$athf.Deg2Rad;
with
float radian = Random.Range(0f, $$anonymous$$athf.PI*2);
As a programmer (or mathematician) you should get familiar with radians as most math functions that involve an angle work with radians.
@rutter, but if i work in 3d space what i need to modify?
Vector3 PositionInCircumference(Vector3 center, float radius, float degrees)
{
float radians = degrees * Mathf.Deg2Rad;
float x = Mathf.Cos(radians);
float y = Mathf.Sin(radians);
Vector3 position = new Vector3(x, y, 0);
position *= radius;
position += center;
return position;
}
Answer by SirCrazyNugget · Jul 29, 2014 at 11:40 PM
Presuming you're working on a 2D plane (and in C#).
Vector3 pos = new Vector3();
float dist = 5f;
float a = Random.Range(0, 2f) * Mathf.PI;
pos.x = Mathf.Sin(a) * dist;
pos.z = Mathf.Cos(a) * dist;
If you wanted to set specific angles (in degrees) you'll need to add a little more maths.
Vector3 pos = new Vector3();
float dist = 5f;
float angle = 37; //degrees
float a = angle* Mathf.PI / 180f;
pos.x = Mathf.Sin(a) * dist;
pos.z = Mathf.Cos(a) * dist;
Then probably create a quick function for return the position from degrees and distance.
void Start(){
GetPosition(37f, 5f);
}
Vector3 GetPosition(float degrees, float dist){
float a = degrees * Mathf.PI / 180f;
return new Vector3(Mathf.Sin(a) * dist, 0, Mathf.Cos(a) * dist);
}
Your answer
Follow this Question
Related Questions
Fade Material's Transparency Based on Distance 3 Answers
How I can make my camera keep a distance between two objects? 0 Answers
Get position of object that uses Perlin Noise X distance/time back/offset? 0 Answers
Limit the distance that gameobject can travel 1 Answer
Calculate UP Distance Displacement for 3D Infinity Runner Game 4 Answers