Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 natsirt16 · Jul 12, 2015 at 07:58 PM · rotationjavascriptinstantiateraycastprefab

A way to instantiate an item with 90 Degree increments?

I am working on a very complex simulation game, I have code most of it myself, However for things you spawn in such as torches, and unsymmetrical items, I need a way that I can instantiate the prefab and rotate it to face me at 90 degree increments. Such as if I were placing a Cabinet, I don't want it to be the inverted Identity, because if I'm offset from the wall, It will be offset too. Thus part of the cabinet would be in the wall. The only axis that I want the objects to rotate around is the Y axis. Thanks in advance!

PS. All I need help with is to make the part of the Instantiate(Block, hit.point, ------) The ---- is the rotation part I need help with. I need an equation to put in that will always result in 90 degree increments, to the vector rotation, Im fairly sure some Conversion of Quaternions would be involved or something like that.

Comment
Add comment
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

2 Replies

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

Answer by Julien-Lynge · Jul 12, 2015 at 08:02 PM

Hi @natsirt,

The key here is actually to use integers and the modulo operator (%), which gives you a remainder of the division with a number. An example to show this:

 113 % 90 = 23
 113 - 23 = 90

In that example, you take the remainder of 113/90 and subtract it from 113, getting 90. If you did it with (say) 281 instead, it would return the closest multiple of 90, which is 270. So you can take whatever your angle is (and you can use Unity's Random class to generate random angles between 0 and 360) and get it to the nearest 90 degrees with something like this:

 angleIWant = myAngle - (myAngle % 90);

At that point, all you have to do is look through the script reference for Quaternion to see how to turn an angle around the Y axis into a quaternion.

Comment
Add comment · Show 1 · 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 natsirt16 · Jul 17, 2015 at 03:35 PM 0
Share

Thanks It Works

avatar image
0

Answer by runninghead · Nov 02, 2020 at 10:10 PM

I use mudulo yesterday in this script for AI, thanks folks!

 // Script to rotate all selected objects by set increments with the intention that it will be useful for the generation of pattern tiles. Copyright runninghead.com 2020. If you use this script please send me examples of your work, I'd love to see what you can do with it! Any suggestions / bug reports welcome.
 
 // NOTE: Does not currently work through Groupings, so Ungroup your objects before running this script!
 
 aDoc = app.activeDocument;
 aSel = aDoc.selection;
 nSel = aSel.length;// number of objects currently selected in illustrator
 increment = Number(prompt("Rotation degree increments (30, 45, 60, 90, etc?)", 60));
 
 for(i=0; i<nSel; i++)
 {
     randomAngle = Math.floor(Math.random()*360);
     rotationAngle = randomAngle - (randomAngle % increment);
     aSel[i].rotate(rotationAngle, rotationAngle);
 }

, // Script to rotate all selected objects by set increments with the intention that it will be useful for the generation of pattern tiles. Copyright runninghead.com 2020. If you use this script please send me examples of your work, I'd love to see what you can do with it! Any suggestions / bug reports welcome.

 // NOTE: Does not currently work through Groupings, so Ungroup your objects before running this script!
 
 aDoc = app.activeDocument;
 aSel = aDoc.selection;
 nSel = aSel.length;// number of objects currently selected in illustrator
 increment = Number(prompt("Rotation degree increments (30, 45, 60, 90, etc?)", 60));
 
 for(i=0; i<nSel; i++)
 {
     randomAngle = Math.floor(Math.random()*360);
     rotationAngle = randomAngle - (randomAngle % increment);
     aSel[i].rotate(rotationAngle, rotationAngle);
 }
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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Grid Placement In Game 2 Answers

Help~!! Raycast on an instantiated prefab can not work!! 1 Answer

How to instantiate on collision? 0 Answers

Odd Behavior with my Drag and Drop Code 0 Answers

Physics related rotation problem. (a weird one) 0 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