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 pudumaster · Jan 26, 2014 at 08:36 AM · c#rotationinstantiatequaterniondirection

Instantiated objects always facing the center

alt text

As shown in the picture above, I instantiate the same object multiple times each at a random position around the circle. I just can't get the objects to always be facing the center of the circle. Iv'e tried Quaternion.LookRotation (Vector3.zero) since the position of the circle is (0, 0, 0), but it didn't work.

Any suggestions?

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 getyour411 · Jan 26, 2014 at 08:38 AM 0
Share

Show instantiate code please; what you are using to obtain the Vector3 "center" x,y,z pos of your circle or is that really your question?

avatar image pudumaster · Jan 26, 2014 at 08:54 AM 0
Share
 Instantiate (ER, generatePosition (8, 8), Quaternion.LookRotation (Vector3.zero));

Since the circle is at the world coordinates of (0, 0, 0) I thought Vector3.zero would point there

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Jan 26, 2014 at 08:41 AM

LookRotation takes a direction not a position. You can solve your problems one of two ways. Either you can use Transform.LookAt() (which does take a postion), or you can calculate a direction vector for LookRotation(). If the position to look at is always the origin (Vector3.zero), then you can use the negated position you use for placing your object. Without seeing your code, it is hard to be specific, but say you place one of the arrows at (0,0,5). You could do something like:

 var pos = Vector3(0,0,5);
 Instantiate(prefab, pos, Quaternion.LookRotation(-pos));

Again, the LookRotation() will take the negative of whatever position you use to place your Instantiated object.

Comment
Add comment · Show 6 · 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 pudumaster · Jan 26, 2014 at 09:04 AM 0
Share

I tried using your code and my objects just disappeared from the scene - I'm not sure what happened. Here's what I have:

 Vector3 pos = generatePosition (8, 8);    //generates position 8 units away from circle at random angle
             Instantiate (ER, pos, Quaternion.LookRotation (-pos));
avatar image robertbu · Jan 26, 2014 at 09:54 AM 0
Share

While running the app, switch to scene view and take a look at your objects. You should be able to see the orientation. Both LookRotation() and LookAt() depend on the forward of the object (the size facing positive 'z' when the rotation is (0,0,0)) being the side you want to face towards an the specified direction or position. If you are displaying Quads or Sprites for example, that will not be true, and your objects will disappear. This is my guess as to what is going on. If true, I can give you alternate code that works for Sprites or Quads.

avatar image pudumaster · Jan 26, 2014 at 11:10 AM 0
Share

Yeah that'd be great because I'm using sprites. Thanks!

avatar image robertbu · Jan 26, 2014 at 02:56 PM 0
Share
 Vector3 pos = generatePosition(8, 8);
 Vector3 dir = -pos;
 float angle = $$anonymous$$athf.Atan2(dir.y, dir.x) * $$anonymous$$athf.Rad2Deg;
 Instantiate(ER,  pos, Quaternion.AngleAxis(angle, Vector3.forward);

This code assumes that your sprite is constructed so that the part you want to point is on the right side of the sprite when rotation is (0,0,0).

avatar image pudumaster · Jan 26, 2014 at 03:20 PM 0
Share

Thanks so much! It worked apart from that dir had to be equal to +pos. Nonetheless, thanks for your help

Show more comments
avatar image
0

Answer by limaoscar · May 18, 2018 at 10:21 AM

i know this is an old post but if anyone still needs to do something similar here is some code.

void DrawCircleVectors(int points, float radius, Transform center1) { float slice = 2 Mathf.PI / points; for (int i = 0; i < points; i++) { float angle = slice i; float newX = (float)(center1.position.x + radius Mathf.Cos(angle)); float newz = (float)(center1.position.z + radius Mathf.Sin(angle)); Vector3 p = new Vector3(newX, center1.position.y, newz); GameObject go = Instantiate(prefab,p,Quaternion.LookRotation(p,center1.position)); go.transform.SetParent(center1); } }

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

19 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

Related Questions

Particle System Instantiate's With Original Rotation - C# 1 Answer

Rotating a certain axis offsets the other ones? 1 Answer

Flip over an object (smooth transition) 3 Answers

i want to get my to cube to rotate 90 degrees on the x-axis as it jumps 0 Answers

Object instantiate, Projectile problems 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