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 /
avatar image
0
Question by jrj2211 · Mar 24, 2018 at 11:52 PM · transformvector3worldspacelocalposition

Class member Vector3 based on local transform

Background:

I'm making prefabs for models that represent different parts of a street (straight, curved, 3 way intersection, 4 way intersection). With this I'm also including nodes on how cars can drive on the tile. Right now I've switched from manually placing a bunch of nodes to simulate a curve and instead defining tangent handles. Each node keeps a list of its neighbors that it can go to, and this "Neighbor" class contains the point, distance and tangent handles.


 public class Neighbor
     {
         public WayPoint point;
         public float distance;
         public Vector3 handleStart;
         public Vector3 handleEnd;
 
         public Neighbor(WayPoint point) : this(point, point.transform.position, point.transform.position)
         {
         }
 
         public Neighbor(WayPoint point, Vector3 handleStart, Vector3 handleEnd)
         {
             this.point = point;
             this.distance = 0;
             this.handleStart = handleStart;
             this.handleEnd = handleEnd;
         }
 
         public void UpdateDistance(Vector3 from)
         {
             this.distance = Vector3.Distance(this.point.transform.position, from);
         }
     }


The problem:

My issue is that when making the prefab I'm entering the Vector3 positions of each handle, but then upon rotating the prefab its not also rotating where the handle should be so my curves are getting all messed up.


  • Degree Rotation (correct)* alt text

90 Degree Rotation (incorrect) alt text


Question:

My thinking is that I may have to check the rotation of the prefab and figure out some calculation to get the proper rotation, but at the same time I feel like there has to be something Unity already has to do this (since it does it for other things such as the node gameobjects). Another option is I could create gameobjects for each handle but this seems excessive. Is there anyway to tell unity that a Vector3 is relative to the gameobject and not the world space?

rotate-0.png (156.8 kB)
rotate-90.png (152.0 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 Bunny83 · Mar 25, 2018 at 12:31 AM

You define your "handles" / tangents in localspace of a tile. To get the worldspace direction you just use transform.TransformDirection() to convert the local space direction into a worldspace direction.

However since you already use gameobjects as nodes you may want to simply rotate the nodes so the nodes forward vector represents the desired direction. You may want to always make the forward vector points along the travel direction. So the start tangent would point "inwards" while the end node would point "outwards" (inward the next tile). So when connecting the tiles the two nodes at the connection would have the same direction. This way you don't need to manually specify a vector but only need to specify a "length" of the tangent to get the correct curve. That means the start tangent would be simply

 transform.forward * neighbor.startLength;

and the end tangent would be

 -neighbor.point.transform.forward * neighbor.endLength;

Note the minus since for the curve we need the tangent into the other direction.


Here's an image of what i meant:

4way intersection


4wayintersection.png (5.2 kB)
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 jrj2211 · Mar 25, 2018 at 07:48 PM 0
Share

That's a really clever solution only needing the length and calculating the handle vector automatically. Works perfect

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

109 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

Related Questions

localposition not working? Calculating a vector in front of an object. Picture and code included 1 Answer

Locking gameobject to rotating floor 0 Answers

Finding the forward vector of a child in local space? 3 Answers

How can I get an OverlapBox with the exact same size and position as a BoxCollider? 1 Answer

Finding the Angle Between Two Objects in 360 Degrees. 3 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