Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 guy_unity879 · Jan 06, 2019 at 06:14 AM · coordinatesworldspaceworldworld spaceworld coordinates

Updating world coordinate according to world points

Hi everybody!
I have found two points lying on a plane, let's say:
worldPoint1 coordinates would be (0,-0.5,1)
worldPoint2 coordinates would be (2.5,-0.5,1)
As far as I understand, the Y coordinates of those two points are identical since they exist on the same plane and are equal to -0.5 since the plane is 0.5m down from the world point where I opened my app. X and Z coordinates are relative to the point I opened the app too.
Now, since I have two points on a plane, I can define my space in an absolute way, right? That way I won't be dependent on the point the camera was when I opened my app and when I "tell" an object to move "right" it will go from worldPoint1 to worldPoint2 no matter where I opened the app and where I stood while clicking the points.
So, what I want to do is aligning the unity's world coordinate system, so the (0,0,0) will be at worldPoint1, (2.5,0,0) will be at worldPoint2 (2.5 cause there is 2.5m distance in the real world between those two points) and up direction will be the normal to the surface.
How can I do that? :)
Thanks!

Comment
Add comment · Show 4
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 Ady_M · Jan 06, 2019 at 11:00 AM 0
Share

Do you have the Normal (Vector3) of the surface? Or do you at least have its rotation? As far as I know, you cannot calculate a Normal from the data you've presented.

Is the surface's up axis always aligned with the world's up axis?

avatar image guy_unity879 Ady_M · Jan 08, 2019 at 05:39 AM 0
Share

Since I use the ground as a plane, the surface's up axis always aligned with the world's up axis. I guess it solves the problem of finding the normal...

avatar image troien · Jan 06, 2019 at 12:05 PM 0
Share

Not sure if I understand the question, but if I understand you correctly, the 'plane' is fictional and only exists in your head? the points are already world coordinates and not plane coordinates.

Because the way I read this question is like: I want to have all positions relative to worldPoint1. So that I can put worldPoint1 at 0,0,0 and the rest around it. If that's true, then just substract worldPoint1 from worldpoint2 and you got your desired point...

     Vector3 worldPoint1 = new Vector3(0, -0.5f, 1);
     Vector3 worldPoint2 = new Vector3(2.5f, -0.5f, 1);
     Vector3 worldPoint3 = new Vector3(2.5f, -2.5f, 1);
     Vector3 worldPoint4 = new Vector3(0, 2.5f, 1);
     
     Vector3 newpoint1 = Vector3.zero;
     Vector3 newpoint2 = worldPoint2 - worldPoint1;
     Vector3 newpoint3 = worldPoint3 - worldPoint1;
     Vector3 newpoint4 = worldPoint4 - worldPoint1;
avatar image guy_unity879 troien · Jan 08, 2019 at 08:04 PM 0
Share

Thank you for the answer! I took the idea of your code and now it looks like this:

 newPos = newPos - Vector3.Cross(secondWorldPoint - firstWorldPoint, Vector3.up) * 0.01f;

or:

 newPos = newPos + (secondWorldPoint - firstWorldPoint) * 0.01f;

It works. I just thought it would be more elegant to rotate the world around a vector than rotate each vector around that vector

1 Reply

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

Answer by Ady_M · Jan 08, 2019 at 06:05 PM

Maybe this is what you had in mind?

 

Create an empty object and attach this script.

It will create 3 test objects for you.

 

Use WSAD to move the green cube according to the red and blue cubes (worldpoint 1 and 2).

 

As you wrote, moving right goes from worldpoint1 towards worldpoint2.

Moving the red and blue cubes will affect the "right" axis.

 

 // The two world point vectors you mentioned
 public Vector3 worldPoint1;
 public Vector3 worldPoint2;

 // (TEST) These two are used to move the world points around while testing
 public Transform worldPoint1_Transform;
 public Transform worldPoint2_Transform;
     
 // (TEST)
 // This object can be moved using the keys
 public Transform movingObject_Transform;
 public float     movingObjectSpeed = 3;


 private void Update ()
 {
     // You may not need these 2 lines. I had two objects in the scene as world points
     worldPoint1 = worldPoint1_Transform.position;
     worldPoint2 = worldPoint2_Transform.position;

     // Calculate the forward rotation from the two world points
     var rotationForward = Quaternion.LookRotation (worldPoint2 - worldPoint1) * Quaternion.Euler (0,  -90,  0);
         
     if (Input.GetKey (KeyCode.W))
     {
         // Move forward
         movingObject_Transform.position += rotationForward * Vector3.forward * movingObjectSpeed * Time.deltaTime;
     }

     if (Input.GetKey (KeyCode.S))
     {
         // Move backward
         movingObject_Transform.position += rotationForward * -Vector3.forward * movingObjectSpeed * Time.deltaTime;
     }

     if (Input.GetKey (KeyCode.A))
     {
         // Move left
         movingObject_Transform.position += rotationForward * -Vector3.right * movingObjectSpeed * Time.deltaTime;
     }

     if (Input.GetKey (KeyCode.D))
     {
         // Move right
         movingObject_Transform.position += rotationForward * Vector3.right * movingObjectSpeed * Time.deltaTime;
     }
 }


 private void Awake ()
 {
     // (TEST)
     // Creates 3 test cubes in the scene: "WorldPoint 1", "WorldPoint 2" and "Moving Object"

     Transform spawnContainer = new GameObject ("Test Objects").transform;
     spawnContainer.parent = transform;

     worldPoint1_Transform  = GameObject.CreatePrimitive (PrimitiveType.Cube).transform;
     worldPoint2_Transform  = GameObject.CreatePrimitive (PrimitiveType.Cube).transform;
     movingObject_Transform = GameObject.CreatePrimitive (PrimitiveType.Cube).transform;

     worldPoint1_Transform.name  = "WorldPoint 1";
     worldPoint2_Transform.name  = "WorldPoint 2";
     movingObject_Transform.name = "Moving Object";

     worldPoint1_Transform.parent  = spawnContainer;
     worldPoint2_Transform.parent  = spawnContainer;
     movingObject_Transform.parent = spawnContainer;

     worldPoint1_Transform.position  = new Vector3 (0, 0, 5);
     worldPoint2_Transform.position  = new Vector3 (5, 0, 0);
     movingObject_Transform.position = new Vector3 (0, 0, 0);

     worldPoint1_Transform.GetComponent <Renderer> ().material.color  = Color.red;
     worldPoint2_Transform.GetComponent <Renderer> ().material.color  = Color.blue;
     movingObject_Transform.GetComponent <Renderer> ().material.color = Color.green;
 }



Comment
Add comment · Show 2 · 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 guy_unity879 · Jan 08, 2019 at 08:07 PM 0
Share

AWESO$$anonymous$$E! It works really good! Thank you very much for arranging my code and more important- my thoughts :)

avatar image Ady_M guy_unity879 · Jan 08, 2019 at 09:45 PM 0
Share

You're welcome :)

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

100 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

Related Questions

How do I convert a point in the world to coordinates on a world space canvas? 0 Answers

Split the screen for multiplayer 1 Answer

How can I get Rigidbody.MoveRotation to rotate in world space on two axes? 1 Answer

Transform's position wherever the mouse is pointing. 2 Answers

From 2D UI coordinates to WorlSpace 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