Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by RonDQuigley · Jun 23, 2016 at 10:41 AM · c#rotationmeshvertexpoint

Rotate a point around a mesh vertex

I've hit a bit of a bump in something I've been looking to try out.

I'm looking to have a point be rotated around a vertex. Whilst I can successfully get a mesh point to be rotated, it keeps rotating around the world space origin: I guess because of the use of Vector3.forward in my code below:

 using UnityEngine;
 using System.Collections;
 
 public class PlaneController : MonoBehaviour
 {
 
     private Mesh mesh;
     public float gizmoSphereSize = 1f;
     Vector3 rotatedPoint;
     Vector3 rotatedVert;
     Vector3 newPos;
 
     void Update()
     {
 
         mesh = GetComponent<MeshFilter>().mesh;
         rotatedVert = transform.TransformPoint(mesh.vertices[0]);
         Vector3 dir = rotatedVert - (transform.position);
         newPos = Quaternion.Euler(Vector3.forward * Time.time * 100) * dir;
         rotatedPoint = newPos + transform.position;
 
         // alternative
 
         /*rotatedPoint = transform.TransformPoint(mesh.vertices[0]);
         rotatedPoint = Quaternion.Euler(Vector3.forward * Time.time * 100) * rotatedPoint;*/
 
 
         Debug.DrawRay(transform.position, rotatedPoint, Color.black);
 
     }
     private void OnDrawGizmos()
     {
         Gizmos.color = Color.red;
         Gizmos.DrawSphere(rotatedPoint, gizmoSphereSize);
     }
 
 }
 

The result is it looks like this:

https://drive.google.com/file/d/0B1wVSPywFDMaSGlTb2hwNlhYYXc/view?usp=sharing

When what I'm wanting is this:

alt text

Any ideas on how to set this up properly?

proper.png (269.3 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

1 Reply

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

Answer by RonDQuigley · Jun 23, 2016 at 12:48 PM

Solved it! In the end it was a completely simple fix I overlooked. Most transformation tutorials I've read that involve rotating around an arbitary point miss out an additional step you need to do if you want the above result. The extra step is that along with resetting the vert (which is your axis of rotation) back to its original position, you then need to offset the rotating vert by the computed vertex direction. I've cleaned up the code and added comments in case this helps others:

 using UnityEngine;
 using System.Collections;
 
 public class PlaneController : MonoBehaviour
 {
 
     private Mesh mesh;
     private Vector3 dirVert;
     public Vector3 origin;
     public float gizmoSphereSize = 1f;
     public float offset;
     Vector3 rotatedVertReset;
     Vector3 vertToRotate;
     Vector3 rotatedVert;
 
     void Update()
     {
         // get the mesh we are working with
         mesh = GetComponent<MeshFilter>().mesh;
         // get the vert in world space
         vertToRotate = transform.TransformPoint(mesh.vertices[1]);
         // get the vert direction in relation to the transform's origin
         dirVert = vertToRotate - (transform.position);
         // rotate the vert around whatever axis around the origin
         rotatedVert = Quaternion.Euler(Vector3.forward * Time.time * 100) * dirVert;
         // reset the vert back to its original position
         rotatedVertReset = rotatedVert + transform.position;     
         // EXTRA STEP - offset the rotated vert by the previous vert direction
         Vector3 rotatedVertWithOffset = rotatedVertReset + dirVert;
 
         Debug.DrawRay(vertToRotate, rotatedVertReset, Color.black);
 
     }
     private void OnDrawGizmos()
     {
         Gizmos.color = Color.red;
         Gizmos.DrawSphere(rotatedVertReset + dirVert
             , gizmoSphereSize);
     }
 
 }
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

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

Related Questions

Mesh Vertices not rotating with object 0 Answers

Set Rotation 1 Answer

Point on a triangle. 1 Answer

Vertex colors not working in shader graph. 0 Answers

Complex if and/or statement for finding neighboring triangles in a mesh 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