Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
1
Question by Sendatsu_Yoshimitsu · Nov 23, 2014 at 02:20 AM · c#mathcircle

Programmatically specifying a random point in an arc in front of the player

I'm trying to get NPCs to consistently spawn in front of the PC, and to this end I'm trying to randomly select a point in an arc in front of the PC. To that end, I've been doing this:

 Vector3 RandomPointInCircle(Vector3 center, float radius, float angle){    //Draws circle of radius, with center center, and locates a point on that circle within angle angle     
         Vector3 position;
         position.x = center.x + radius * Mathf.Sin (angle * Mathf.Deg2Rad);
         position.y = center.y + radius * Mathf.Cos (angle * Mathf.Deg2Rad);
         position .z = center.z;
         return position; 
     }

I thought that passing it the player's position, a radius of 5, and an angle of Random.range(45, -45) would result in enemies spawning at random points within a 90 degree arc in front of the player, but every enemy spawns at the same point, to the player's right- is my math slightly borked?

Comment
Add comment · Show 2
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 Kiwasi · Nov 23, 2014 at 02:35 AM 0
Share

Haven't looked at the math. But one quick way to tell if you math is off would be to write a loop that passes in angles from -45 to 45, say five apart, and makes a cube at those positions. Then you can at least check if its this method or your random generator that's misbehaving.

avatar image robertbu · Nov 23, 2014 at 02:39 AM 0
Share

For polar to rect, you have your sin and cos backwards. It cos for x and sin for y...but this is likely not your problem since the results would not be the 'same point'.

1 Reply

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

Answer by Bunny83 · Nov 23, 2014 at 02:38 AM

Your code should work just fine, however keep in mind that when in 3D the y-axis is up. So since your arc is on the x-y plane, the point will be displaced on the x axis and the y-axis. If you want a point on the x-z-plane you should use z and not y in your code.

Also keep in mind that you work in worldspace and not relative to a certain object. (just saying since you said "in front of the player")

edit

To specify a point in localspave you either have to write your method a bit more general and then use the Transform's TransformPoint method, or directly use the transforms local axis in the calculation:

 Vector3 RandomPointInCircle(float radius, float angle){
     float rad = angle * Mathf.Deg2Rad;
     Vector3 position = new Vector3(Mathf.Sin( rad ), 0, Mathf.Cos( rad ));
     return position * radius;
 }
 
 // Now use like this:
 var localPoint = RandomPointInCircle(5,angle);
 var worldPoint = transform.TransformPoint(localPoint);
  

The other way would be to pass a Transform reference to the method and use it like this:

 Vector3 RandomPointInCircle(Transform trans, float radius, float angle){
     float rad = angle * Mathf.Deg2Rad;
     Vector3 position = trans.right * Mathf.Sin( rad ) + trans.forward * Mathf.Cos( rad );
     return trans.position + position * radius;
 }
Comment
Add comment · Show 3 · 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 Sendatsu_Yoshimitsu · Nov 23, 2014 at 02:43 AM 0
Share

Oooh, that explains so much- thank you! :)

Is there a way to specify local space, or would I need to do my calculation normally than adjust it for my PC.forward?

avatar image Bunny83 · Nov 23, 2014 at 03:33 AM 0
Share

@sendatsu: I've edited my answer ;)

avatar image Sendatsu_Yoshimitsu · Nov 23, 2014 at 04:58 AM 0
Share

Thank you so much, I really appreciate your help :)

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

28 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 avatar image avatar image

Related Questions

A problem with intersection detection 1 Answer

Clarification on how RangeAttribute works 1 Answer

What's a more efficient way to sort an array into multiple groups? 1 Answer

Finding the axis of a curving pipe 0 Answers

A fast triangle triangle intersection algorithm for unity? 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