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 marioslokas · Jun 20, 2019 at 02:33 PM · rotationtransformtranslationvector math

Aligning a transform according to two different positions and directions

I, unfortunately, am quite bad with vector math and have been dealing with this for some time now. I will attempt to explain in hopes of getting an explanation:

I have 5 different rooms in my AR game that the player can navigate into. Each room has an empty gameobject as a child to it, that represents the player's spawn point when they arrive into the room. The 5 different rooms are all game objects I enable and disable depending on the room the player moves into.

Every time the player (the camera in this case) moves to a room, I want the player to appear directly over the spawn point specified in the room. This is achieved by the following code:

 Vector3 spawnPoint = thisRoom.RoomSpawnPoint;
 Vector3 point = new Vector3(_camera.transform.position.x, _roomWorldPosition.y, _camera.transform.position.z);
  
 _currentEnvironment.transform.Translate(point - spawnPoint);

where _roomWorldPosition.y is set by a detected plane's y value.

My next task is to make sure the room is rotated properly so that the room's spawn point forward vector is aligned with the camera's forward vector. However, the below code sample does not work as expected:



 Vector3 cameraFlatForward = new Vector3(_camera.transform.forward.x, 0f, _camera.transform.forward.z);
 _currentEnvironment.transform.rotation =
             Quaternion.Euler(new Vector3(0f, Vector3.Angle(cameraFlatForward, spawnPoint)));





The rooms are rotated in random angles and seem to spawn further and further away. For clarification, first I apply the rotation and then the translation. Any help appreciated.

EDIT: Using the Translate function I am able to always transport the room to the place I want it to, so it always appears beneath the camera and gives the feeling of the player spawning on the right point.

 _currentEnvironment.transform.Translate(point - thisRoom.RoomSpawnPoint);

Using Quaternion.FromToRotation (without translating the room) the room always looks towards the direction the camera is looking, so the spawn point's forward vector is aligned with the camera's flat forward vector.

 _currentEnvironment.transform.rotation = Quaternion.FromToRotation(thisRoom.RoomSpawnPointForward.normalized, cameraFlatForward.normalized);

However, applying these at the same time never works the way I want it. The room keeps appearing in wrong places and wrong rotations. I also make sure the position and rotation of my room gameobjects are reset every time the player moves from room to room.

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
1

Answer by Namey5 · Jun 23, 2019 at 02:06 PM

If you want to align a rotation with a vector, you can use the LookRotation function. To align a transform with another, you can take the angle between them and use it as an offset to the rotation. Unity's Vector3.Angle function is unsigned, meaning it simply finds the smallest angle between the two vectors and throws away any details about direction. This means that the angle it returns may not be for the direction you are rotating, hence the need to apply it 1-3 times.

 Vector3 cameraFlatForward = new Vector3 (_camera.transform.forward.x, 0f, _camera.transform.forward.z);
 Vector3 angle = Quaternion.LookRotation (cameraFlatForward).eulerAngles;
 Vector3 off = thisRoom.RoomSpawnPoint.rotation.eulerAngles - _currentEnvironment.transform.rotation.eulerAngles;
     
 //If the rotation still appears incorrect, switch the '+ off' to '- off'
 _currentEnvironment.transform.rotation = Quaternion.Euler (angle + off);

Comment
Add comment · Show 7 · 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 marioslokas · Jun 25, 2019 at 08:49 AM 0
Share

Hey, thanks for the answer. Turns out this doesn't work as I wanted it to... The room still spins to wrong directions.

avatar image Namey5 marioslokas · Jun 25, 2019 at 08:55 AM 0
Share

In what sense are they pointing in wrong directions? The above code should align the transform with the camera, but if the mesh is aligned differently to the transform, then it won't point in the right direction and you'll need to make some adjustments to either the mesh, object or alignment vector.

avatar image marioslokas Namey5 · Jun 25, 2019 at 09:03 AM 0
Share

It's hard to say in what sense they are pointing wrongly. The rotation appears random. The mesh is aligned differently with the transform. After all it's an entire environment with lots of different props around. That is why I use a spawn point gameobject as the required position and orientation the camera should have (if the camera could move at all). What baffles me is that the Quaternion.RotateAround function should do what I want, as I can make the environment rotate around the spawn point after I translate the room to the camera. However, I again get random rotations.

Show more comments

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

161 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

Related Questions

Translating measurement in rotation speed and game object size to non-technical staff 0 Answers

is there any possible to use two transform.rotate function for same gameobject?? 2 Answers

Moving objects together without parenting 5 Answers

Why does this script work correctly? 1 Answer

Smooth transform position and rotation 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