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 molosev · Dec 01, 2014 at 10:24 AM · verticesworldspacegeometrylocalspace

Move cube vertices up 2 units (world) whatever cube orientation

Here's my question

I have a cube

I rotate the cube on its x, y and z axis every frame

Finally I'd like to get the image of this cube but 2 units up the original cube. When I say image I mean that the vertices of the cube are translated to their new coordinates. (see picture)

alt text

How should I go about doing that... Any ideas ?

Edit Title: changed title from "3d trigonometry question" to "Move cube vertices up 2 units (world) whatever cube orientation". Far more specific ;)

sans titre-1.png (11.5 kB)
Comment
Add comment
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

2 Replies

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

Answer by HarshadK · Dec 01, 2014 at 10:58 AM

If you want to move the cube upwards in the world space then you can translate it by using Vector3.up. Something like:

 transform.position += transform.position + (Vector3.up * 2);

This will shift the cube upwards in the world 'y' axis by 2 units, keeping its rotation.

Comment
Add comment · Show 11 · 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 molosev · Dec 01, 2014 at 11:05 AM 0
Share

Yep, except that I don't want the cube to be translated but only its vertices..

avatar image HarshadK · Dec 01, 2014 at 11:08 AM 0
Share

How can you translate the vertices of a mesh but keep the mesh there itself?

Do you mean you just want to get the new position values for the cube vertices?

In that case you can translate the cube to new position as specified in the answer. Then get the position of its vertices using the $$anonymous$$esh class and then reset the position of the cube back down by 2 units. Something like:

Psuedocode:

 // $$anonymous$$ove up by 2 units
 transform.position += transform.position + (Vector3.up * 2);
 
 //*** Get the position of vertices of the mesh using $$anonymous$$esh class
 
 // Reset the position of the cube
 transform.position += transform.position - (Vector3.up * 2);

avatar image molosev · Dec 01, 2014 at 11:16 AM 0
Share

Yes, I want to know the new position values for the cube vertices, but in local space. The transform of the mesh should keep its position and orientation...

avatar image HarshadK · Dec 01, 2014 at 11:28 AM 0
Share

You can use the above method which will provide you with the location of the vertices relative to the center of the object. You can then use transform.localToWorld$$anonymous$$atrix to convert it to the world positions.

avatar image molosev · Dec 01, 2014 at 11:32 AM 0
Share

But if I do as you say I will get that :

alt text

because the vector3.up will translate the transform on its local axis and the cube is already rotated by 45°...

sans titre-2.png (10.3 kB)
Show more comments
avatar image
0

Answer by molosev · Dec 03, 2014 at 04:38 PM

I finally found a solution without cube translation:

 using UnityEngine;
 using System.Collections;
 
     public class MoveVerticesUp : MonoBehaviour {
     
         Mesh mesh = null;
         Vector3[] localVerticesArray = null;
         Vector3[] globalVerticesArray = null;
     
         void Start () {
 
             mesh = GetComponent<MeshFilter>().mesh;
             localVerticesArray = mesh.vertices;
             globalVerticesArray = mesh.vertices;
 
         } // end Start
             
         void Update () {
             
             for(int i = 0; i < localVerticesArray.Length; i++)    
             {    
                 globalVerticesArray[i] = transform.TransformPoint(localVerticesArray[i]);
 
                 globalVerticesArray[i].y = globalVerticesArray[i].y + 2;
 
                 globalVerticesArray[i] = transform.InverseTransformPoint(globalVerticesArray[i]);
             }
             
             mesh.vertices = globalVerticesArray;
             mesh.RecalculateBounds();
             mesh.RecalculateNormals();
 
         } // end Update
         
     } // end class    


Works like a charm !

Thanks HarshadK for giving me tools and general flow!

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

26 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

vertices to world position 1 Answer

Specific use of TransformDirection 0 Answers

vertex shader moving vertex up in world space 1 Answer

WorldSpace movement? 1 Answer

What is the difference between TransformDirection, TransformVector and TransformPoint since they all accept and return a Vector3 value. 1 Answer


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