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 Alismuffin · Nov 26, 2012 at 08:41 PM · rotationmeshcube

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);
         }
Comment
Add comment · Show 5
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 code-blep · Nov 26, 2012 at 08:43 PM 0
Share

I think it's down the mesh you are using. Did you import it?

avatar image Alismuffin · Nov 26, 2012 at 09:15 PM 0
Share

No I am creating the cube at runtime via C# coding

avatar image code-blep · Nov 26, 2012 at 09:24 PM 0
Share

Hmmm. You might want to post your script here so we can see what is going on....

avatar image code-blep · Nov 26, 2012 at 10:05 PM 0
Share

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?

avatar image Alismuffin · Nov 26, 2012 at 10:16 PM 0
Share

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)

2 Replies

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

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);
         }
Comment
Add comment · Show 1 · 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 rosdi · Dec 21, 2014 at 03:07 AM 0
Share

Thanks for this!.. also you could replace that new Vector3(0,1,0) part to just Vector3.up

avatar image
0

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.

Comment
Add comment · Show 3 · 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 Alismuffin · Nov 26, 2012 at 10:18 PM 0
Share

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

avatar image code-blep · Nov 26, 2012 at 10:20 PM 0
Share

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.

avatar image Alismuffin · Nov 26, 2012 at 10:22 PM 0
Share

The pivot is fine in the editor however on the created cube I g2g now, thanks for helping :)!

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

11 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

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


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