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
0
Question by Woj_Gabel_FertileSky · Jan 22, 2012 at 04:31 AM · transformlocalworldquaternions

world direction to local when instantiating

Hi! I am trying to get some 3d menu going. I have found a script that does what i want - instantiate object around center in circle:

 if(Input.GetKeyUp(KeyCode.LeftAlt))
 {
     
         var center = my3dCamera.transform.position;

            for (i = 0; i < 5; i++)
            {
         var pos = RandomCircle(center, 2);
         
         // make the object face the center
         //var rot = Quaternion.FromToRotation(Vector3.forward, center-pos); //not needed
         var clone = Instantiate(button, pos, transform.rotation); //rot
         }
 }

 function RandomCircle(center:Vector3, radius:float): Vector3 {
 // create random angle between 0 to 360 degrees
 var ang = Random.value * 360;
 var pos: Vector3;
 pos.x = center.x + radius * Mathf.Sin(ang * Mathf.Deg2Rad);
 pos.y = center.y + radius * Mathf.Cos(ang * Mathf.Deg2Rad);
 pos.z = center.z;
 return (pos);

}

i have attached this script to main camera. Problem is that i cant get my camera local direction. center variable is always pointing in the world rotation(or direction). How can i pass "center" to RandomCircle function in it's (camera this script is attachet to) local direction?

I have tried transform.localRotation, transform.TransformDirection, InverseTransformDirection, but nothing works. They are instantiated off screen, but always facing world coords.

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

1 Reply

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

Answer by syclamoth · Jan 22, 2012 at 06:44 AM

Try this-

 var pos =  my3dCamera.transform.TransformPoint(RandomCircle(center, 2));

And then after you've instantiated the cube to rotate it so it faces the camera, use

 clone.transform.LookAt(my3dCamera.transform);

This way, your circle will align with the camera's local axes, and the cubes will face the camera.

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 Woj_Gabel_FertileSky · Jan 22, 2012 at 02:26 PM 0
Share

Thanks, but I tried something along this and it didnt work. Same with your code. The circle center is ok (tilted in the local z axis of camera) but its way off screen. Something with scale maybe?

avatar image Woj_Gabel_FertileSky · Jan 22, 2012 at 02:53 PM 0
Share

I tried to test this with simpler code:

 var center : Vector3 = my3dCamera.transform.position;
         center.z = transform.position.z + 5;
         var clone = Instantiate(button, center, transform.rotation);
         clone.transform.parent = transform;

Cude is facing ok, but is instantiated in global pos of myCamera. So position, or rotation of instantiated cubes must be set before instantiate. After adding:

 clone.transform.localPosition = center;

the cube is instantiated off screen.

avatar image syclamoth · Jan 22, 2012 at 02:55 PM 1
Share

Oh, I just realised something. The 'center' variable is a trap- you should just be using Vector3.zero for that! Just calculate the offsets, and use TransformPoint to make the offsets relative to the camera, in local coordinates.

avatar image Woj_Gabel_FertileSky · Jan 22, 2012 at 03:12 PM 0
Share

wait, so center should be (0,0,0)? If so, then cubes will be made in world center, and... ...

OH GOD! IT WOR$$anonymous$$S!!

Thank you!! And +1 internetz for you because star wars reference :)

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

bone rotations changed in 4.3.2 inspector? 0 Answers

Local to World space 0 Answers

Transform.Rotate for Quaternions? 1 Answer

Objects local/world cordinate question 1 Answer

Transform.right misunderstanding 2 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