Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 EpicPotato · Mar 29, 2014 at 07:09 PM · rotationphysics2dsprites

2D Physics Rotate Sprite Level With Floor

I need to, using the 2D Physics, make my player rotate so that it is level with the floor. I've seen solutions before, but all of the use 3D Physics. I'm using 2D Physics and my camera is facing positive Z. I can't just set the sprite's Z to the ground's though, I need to use the transform.normal or something.

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

Answer by robcbryant · Mar 29, 2014 at 07:21 PM

You shouldn't need to be worrying about the z at all--in transformation terms. I'm not entirely sure what you're getting at--but I think I do. If your sprite is a quad (a flat square) and you are rotating it with physics all the time and need to 'reset' it to be flat with the ground couldn't you just set the rotation(I think the z axis) back to zero?

Here's a quick easy fix.

 //this goes before the Update() loop because you don't want it changing every update
 private Quaternion zeroRotation = whateverGameObject.transform.rotation;
 
 //when it's time to make the sprite 'level' with the ground again just set the rotation back to this
 
 whateverGameObject.transform.rotation = zeroRotation;


Dealing with rotations can be difficult--when I can I try to use a placeholder Quaternion variable rather than figure out the very complicated math behind it.

*EDIT: I think now--you mean you want the physics to auto rotate the sprite back to 'zero' --not just change it immediately to zero -- ie. a smooth turn to zero. You can use the same placeholder quaternion for your zero position and rotate the sprite along the axis in question(I'm assuming z) and check if it's at the zero position or not--if it is then stop rotating.

 Update() {
 //check if the transform is at the zero rotation
 if(transform.rotation != zeroPosition)
    // Slowly rotate the object around its z axis at 1 degree/second.
    transform.Rotate(Vector3.up * Time.deltaTime);
 
 }

I will say that if physics are involved--your quaternion may not be an integer--so you would potentially never be equal to the zeroPosition quaternion. You can look into doing a looser comparison with some buffer room ie. 1 degree of wiggle room--but this is a quick answer. Note that this will always attempt to rotate the sprite back to zero indefinitely--so you may want to set a trigger for it specifically--I don't know all the details.

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 robcbryant · Apr 01, 2014 at 01:38 AM 0
Share

Did this work?

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

21 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

Related Questions

How to get a ball with physics2d to move instead of spin? 1 Answer

Rotating Children as Rigidbodies2D because transform rotation is too costly 0 Answers

How to rotate my 2d gameObject using touch input?? 1 Answer

Photon Syncing rotating projectile 2D 1 Answer

Making sprites just rotate and not tilt. 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