Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by Ulftek · Mar 31, 2019 at 04:14 PM · rotationdirection

Need help getting starting directions within a certain range

I'm trying to get my head wrapped around rotations work. In one respect, there is the rotation that is the spin of an object, but there is another rotation that is what direction the object is facing. It is the latter that I need help with. I have object spawning off screen that I want to come onto the screen. That means depending on which side of the screen they are spawning, they need to only go within a certain range of directions.

I am working in a 3D environment, but I'm making it work as a top-down 2D shooter game. This means that Z+ is "up", Z- is "down", X+ is "right," and X- is "left".

I've done quite a bit of searching online, but unfortunately nothing I've found has helped. I'm wanting to keep my objects moving on the X and Z planes, but again, only within a certain range based on where they are coming from. I have my code already set up to spawn them where they need to be, I just can't figure out this direction issue.

EDIT: I managed to figure it out with Euler by doing this:

 spawnRotation = Quaternion.Euler(0, Random.Range(130f, 230f), 0);

And I'd just put the angle range (between 0-360) where I wanted it to come from. In the above case, that is what I used for my angles when my objects spawn at the top of the screen.

alt text

directions.png (26.0 kB)
Comment
Add comment · Show 4
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 RobAnthem · Mar 31, 2019 at 04:24 PM 0
Share

$$anonymous$$aybe what you want is Vector3.Angle? It takes 2 positions and returns the angle of the positions. Best way to do for a terrain map is to make a new position with the old position and remove the y axis, so that it only calculates a flat angle. If the angle is less than 90 degrees it will be in front of the object you are comparing to, and if it is greater, so anything greater than 90 would be out of visual range. A quick example..

 public bool IsVisible(Vector3 origin, Vector3 target)
 {
     origin.y = 0;
     target.y = 0;
     return Vector3.Angle(origin, target) < 90.0f;
 }
avatar image TreyH RobAnthem · Apr 01, 2019 at 02:14 PM 1
Share

Should clarify that Vector3.Angle doesn't produce anything sensible when you provide positions, only directions. An "angle" between positions doesn't make geometric sense.

avatar image RobAnthem TreyH · Apr 02, 2019 at 04:28 AM 1
Share

Yeah I don't know what I was thinking, thanks for the correction :)

avatar image Magso · Mar 31, 2019 at 05:57 PM 0
Share

If you can get it working in one direction, Add the Z rotation by multiple of 90 depending on which side it comes from e.g.

 //from bottom.
 transform.eulerAngles.z = Random.Range(-45, 45);
 
 //if it is spawned from the top
 transform.eulerAngles.z += 180;

0 Replies

· Add your reply
  • Sort: 

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

140 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 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 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 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 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

Set rotation based on 2 points problem 1 Answer

Rotation direction in coroutine 2 Answers

Gun not firing in certain direction 1 Answer

How to incorporate a rotation towards mouse position in this script? I tried 0 Answers

How can I get the rotated position without using Transform in 2D? 1 Answer


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