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 marcusrwatson · Nov 23, 2015 at 12:37 PM · rotationquaternionvelocityrelative position

Instantly rotating and changing an object's velocity relative to another (probably quaternion mistake!)

Hi,

I have a player walking down a corridor A and at a certain point they are teleported into a visually-identical corridor B, however corridor B is at a different orientation than corridor A. The idea is that this should be seamless: the player has no awareness that the teleportation has occurred.

At the end of each corridor is a "keyhole" object that has been instantiated from a prefab. The intended plan is to: 1) compute player's location in keyhole A's local space, change player's location to have the same location in keyhole B's local space. 2) do the same for player's rotation. 3) compute the angle between keyhole A and keyhole B's rotations, then use this to modify the player's current velocity.

I think if I do 1, 2 and 3 properly then everything should appear seamless as desired.

I've got 1 working, I thought I had 2 but the rotation now seems to "jump", and the velocity part definitely isn't working. I'm 90% sure that I've got the basic way of solving each of these problems done in a nice Unity-friendly manner, but I've got two elements in the wrong order or something like that.

Here's my code, using my terminology above keyhole A is teleportKeyhole below, and keyhole B is newKeyhole. Any help with this is much appreciated.

                     Vector3 playerFromKeyholePos = gameObject.transform.InverseTransformPoint(player.transform.position);
             Quaternion playerFromKeyholeRot = Quaternion.FromToRotation(player.transform.forward, gameObject.transform.forward);
             Quaternion newKeyholeFromTeleportKeyholeRot = Quaternion.FromToRotation (gameObject.transform.forward, newKeyhole.transform.forward);
             float newKeyholeFromTeleportKeyholeAngle = Vector3.Angle(gameObject.transform.rotation.eulerAngles,newKeyhole.transform.rotation.eulerAngles);
             player.transform.position = newKeyhole.transform.TransformPoint(playerFromKeyholePos);
             player.transform.rotation = Quaternion.Inverse(player.transform.rotation) * playerFromKeyholeRot * newKeyholeFromTeleportKeyholeRot;
             player.GetComponent<Rigidbody>().velocity = Quaternion.Euler(0, newKeyholeFromTeleportKeyholeAngle,0) * player.GetComponent<Rigidbody>().velocity;
         }

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 marcusrwatson · Nov 28, 2015 at 03:26 PM

OK, I realized that I had completely misread two other Unity Answers posts when writing the above code. I've fixed the problem, using the far simpler code below. Sorry for any confusion.

 Vector3 playerFromTeleportKeyholePos = gameObject.transform.InverseTransformPoint(player.transform.position);
 Quaternion newKeyholeFromTeleportKeyholeRot = Quaternion.FromToRotation (newKeyhole.transform.forward,gameObject.transform.forward);
 Vector3 playerFromTeleportKeyholeVelocity = gameObject.transform.InverseTransformDirection(player.GetComponent<Rigidbody>().velocity);
 player.transform.position = newKeyhole.transform.TransformPoint(playerFromTeleportKeyholePos);
 player.transform.rotation = Quaternion.Inverse(newKeyholeFromTeleportKeyholeRot) * player.transform.rotation;
 player.GetComponent<Rigidbody>().velocity = newKeyhole.transform.TransformDirection(playerFromTeleportKeyholeVelocity);
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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Camera.WorldToScreenPoint Equivalent 1 Answer

Rotation quaternion to angular velocity 2 Answers

Move object in rotating direction. 0 Answers

Problems with rotating an object using angular velocity. 2 Answers

to rotate along z axis 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