- Home /
Instantiated objects always facing the center
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?
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?
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
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.
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));
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.
Yeah that'd be great because I'm using sprites. Thanks!
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).
Thanks so much! It worked apart from that dir
had to be equal to +pos
. Nonetheless, thanks for your help
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); } }
Your answer
Follow this Question
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