- Home /
Rotating a created mesh around it's pivot
Hey guys,
So I am creating a cube at runtime and wanting to be able to rotate it around with the WASD keys. The pivot automagically adjusts to the center of the object seemingly(when I click it the pivot is indeed at the center) yet when I use transform.Rotate it rotates from the very corner of the cube. Not sure what's going on. When I rotate it manually via the inspector it rotates along it's pivot fine
Here is a snippet of the code:
void Update ()
{
if(Input.GetKey(KeyCode.A))
{
chunk.transform.Rotate(new Vector3(0, 1, 0), Time.deltaTime*20, Space.Self);
}
I think it's down the mesh you are using. Did you import it?
Hmmm. You might want to post your script here so we can see what is going on....
I deleted my answer to keep this clear.
So you have no Child/Parent set up so it can't be that.
I have just created a new cube gameobject and applied your code and it works fine for me. So I maybe there is an issue with the way the cube is created at runtime.
Is there any reason why you would do it that way? Would instantiate work better for you?
Yes it must be to do with the creation It's odd though considering Unity editor tells me the pivot is perfectly fine and centered
And yes I am creating it at runtime because i am actually using a voxel technique
Here: http://i.imgur.com/fyzay.jpg
It creates cubes in a grid but only draws the faces that will actually be seen hence why creating the mesh at runtime is required. The mesh will be rebuilding many times during runtime, and I need it to always pivot from it's center(Which is where the pivot is supposedly placed)
Answer by Alismuffin · Nov 27, 2012 at 10:17 AM
SOLVED
Turns out that firstly I was wrong, the pivot was not cenetered, I had just had unity editor set to pivot around center. Knowing that the pivot was misplaced made more sense and led me to discover I could use RotateAround in conjunction with renderer.bounds.center to get the desired result.
Here is my code:
if(Input.GetKey(KeyCode.A))
{
chunk.RotateAround(chunk.renderer.bounds.center, new Vector3(0, 1, 0), Time.deltaTime*20);
}
Thanks for this!.. also you could replace that new Vector3(0,1,0)
part to just Vector3.up
Answer by code-blep · Nov 26, 2012 at 10:15 PM
OK, I have this working for me but in UnityScript. This script is placed in a empty gameobject. Call it whatever you want.:
function Start () {
var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.AddComponent(RotateTest);
}
The RotateTest script is located in a folder called Resources:
function Update ()
{
if(Input.GetKey(KeyCode.A))
{
transform.Rotate(new Vector3(0, 1, 0), Time.deltaTime*20, Space.Self);
}
}
The cube is created and rotates around a centre point.
My example is based upon info from this page which also has C#: http://docs.unity3d.com/Documentation/Manual/InstantiatingPrefabs.html
But maybe give mine a go. If you can;t get it to work I'll upload my project file if you like.
The creation method is a bit more complex in my case as I am not building primitives, but rather i am building a cube(chunk) made up of cubes(voxels)
I am manually creating the vertices locations and tris
O$$anonymous$$. In that case I would say that your creation method is doing something funky to the pivot point. Without the full code, I cannot take this any further Alismuffin.
The pivot is fine in the editor however on the created cube I g2g now, thanks for helping :)!
Your answer
Follow this Question
Related Questions
Generating a cube mesh with two given points 1 Answer
Simple plane generation 1 Answer
Plane on top of rotated cubes 0 Answers
Change Pivot Point- Maya 3 Answers
How to rotate a mesh collider? 0 Answers