Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by sotha_sil · Jul 29, 2014 at 11:00 PM · distancecoordinatesangles

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Owen-Reynolds · Nov 27, 2017 at 03:13 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
7
Best Answer

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;
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sotha_sil · Jul 29, 2014 at 11:41 PM 1
Share

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;

avatar image Bunny83 · Jul 30, 2014 at 12:28 AM 0
Share

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.

avatar image sherciu · Nov 27, 2017 at 12:35 PM 0
Share

@rutter, but if i work in 3d space what i need to modify?

avatar image d2clon · Aug 24, 2021 at 07:32 PM 0
Share
 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;
 }

avatar image
3

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);
 }



Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

26 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges