- Home /
Rotating Cuboid Around Pivot
Hi Guys, I'm looking to rotate my cuboid around a pivot point. I was wondering how to do this (as shown in image(green is the pivot point)) so it rotates around the pivot point and also stops at the 90 degrees. I'm also looking to find a way to do it without setting a gameobject as a child because it will be difficult when I start adding more axis.
Thanks to anyone who can help,
IPS
Answer by flaviusxvii · Feb 19, 2013 at 03:24 PM
I suppose you could move all of the vertexes of the mesh so that that corner is the origin in model space. Then when you rotated the transform it would rotate around that point.
It would get dicey if you want to rotate around multiple axes though.
You could also do the math and figure out the series of positions and rotations necessary to frame-by-frame "fake" a rotation like this. So the object itself would still rotate around it's center, but you'd move it each time you rotate so that it would appear to rotate around that pivot.
Without knowing your use case it's hard to tell what's the best option.
Hi flavi,
I don't think I can create that because of the problem it would have with multiple axes.
This is the sort of thing I'm looking to create: http://www.coolmath-games.com/0-bloxorz/index.html
Any ideas?
Answer by robertbu · Feb 19, 2013 at 04:11 PM
You can use either Transform.RotateAround(), or Quaternion.AngleAxis() to do an angle/axis rotation. You specify the point (i.e. the corner of the cube) and the axis (i.e. the edge) to rotate around. With a cuboid, both are easy to calculate especially if you keep the cube axis aligned. Note the direction of the axis of rotation vector matters.
Ahah, so how exactly would could I translate RotateAround() into code as I need to specify exact point to rotate around (-1,0.5,0 - the green point) and I'm not quite sure how to do that.
I'm assu$$anonymous$$g the coordinate is local to the cube. So for an immediate rotation it will be something like (untested):
v3Pos = new Vector3(-1.0f,0.5f,0.0f);
v3Pos = transform.TransformPoint(v3Pos);
transform.RotateAround(v3Pos, Transform.right, 90.0f);
The point is a world coordinate. I use Transform.right so that the vector will remain accurate for that rotation even if you rotate the cube every which way.
Great! Now it's fixed with the subtracting of the v3Pos = transform.TransformPoint(v3Pos); for some strange reason it made the cuboid enter warp speed. But other than that its working.
Thanks!
Your answer
Follow this Question
Related Questions
Change rotation axis when importing from Maya 2 Answers
C# Rotate More than Two GameObjects 1 Answer
Finding Edges 1 Answer
Rotate object 1 Answer
Functions via camera rotations... 1 Answer