Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 /
  • Help Room /
avatar image
0
Question by colinalaws · Jan 22 at 06:30 PM · rotationtransformquaternionprogramming

How to preserve Y rotation while setting transform.up?

I'm working on a building system in which the player may drag objects and snap them together. Here is a video to show what the snapping is like: Video Showing snapping to the normal by setting transform.up Think Spore or KSP with how a raycast hit to a bindable object (an object you can place something onto) while editing lets you place the object onto its surface by setting the position and Transform.up of the dragged object to the raycast hit's normal and position.

 // Set the upwards direction to the normal of the hit.
 objectBeingDragged.transform.up = closestEditableHit.Value.normal;
 
 // Update the position so it hugs the surface of the object we hit.
 objectBeingDragged.transform.position = closestEditableHit.Value.point;

The issue I am facing comes in where I attempt to rotate the current object being held by the player. Since the object being dragged is setting its transform.up to the raycast hit's normal, I lose the Y-Rotation. Here is a snippet from the Player's code where I rotate an object being held.

 // If this player is holding an object.
 if (ObjectBeingHeld != null)
 {
     // A delta in the object's Y Euler angles to apply
     float delta = 0.0f;
 
     // When using snap mode for perfect angles.
     if (useSnapMode)
     {
         // Increment in intervals of 90 degrees.
         delta = positive ? 90 : -90;
     }
     else
     {
         // Otherwise, the player may make incremental rotations in free edit.
         delta = (positive ? -5 : 5);
     }
 
     // Apply the rotation to the object being held around it's Y axis.
     // ( I have tried both world space and local space here... )
    ObjectBeingHeld.transform.Rotate(ObjectBeingHeld.transform.up, delta, Space.World);
 
 }

The object tries to rotate for just a split second, but gets reset, likely due to the transform.up being set at nearly the same time I'm trying to rotate. I'm trying to wrap my head around how I can express what I'm trying to accomplish.

Also, you will notice in the video, as soon as I pick up the object take note that its x and z rotations are being manually frozen by setting them to 0, while preserving the y rotation.

 if (isBeingDragged)
 {
     // Lock the x and z rotation
     transform.rotation = Quaternion.Euler(0, transform.rotation.y, 0);
             
     if (rb != null)
     {
         rb.velocity = Vector3.zero;
     }
 }

Comment
Add comment · Show 2
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 colinalaws · Jan 22 at 07:20 PM 0
Share

I notice that Spore does not let you rotate until you have stopped moving the object. I'm not sure if that's really the only good way to handle this.

avatar image colinalaws · Jan 22 at 08:47 PM 0
Share

I believe I may be on to something. I'm looking at the problem wrong. I need to simultaneously set transform.right/forward perhaps?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by colinalaws · Jan 22 at 09:11 PM

Eureka! My suspicion was correct. I needed to modify both transform.forward, and transform.up simultaneously.

The solution here is to keep a variable on the object being rotated called "forward" which is a vector 3. Then, when the player clicks a rotate button on the d-pad, I modify the "forward" variable on the object being dragged. That way I keep sort of a cached state if you will of the rotation the player is trying to achieve. Simply swapping it between Vector3.up and Vector3.right seems to work pretty well.

In the end, applying the rotation on the object looks like this in my raycast:

     // The variable forward here being passed in is modified by the player to "rotate" the object.
     transform.rotation = Quaternion.LookRotation(forward, closestEditableHit.Value.normal);

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

261 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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 can I set rotation properly? 0 Answers

How to rotate a camera slowly along a tween? 1 Answer

My cube gets stuck at 0 degrees rotation 1 Answer

Quaternion rotation issue please help :'( 0 Answers

[Solved] Quaternion from IMU sensor to GameObject Orientation problem 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