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 MadTigger · Aug 31, 2014 at 05:42 AM · 2dsprite

Determine sprite facing

I am working with a top down 2d game, and I am having some trouble determining the facing of sprites. The transform object exposes rotation properties, but they are quaternions, and not simple floats indicating how many degrees an object is rotated around a particular axis.

Quaternions are vectors, and rotations around said vector, so if I take the z-vector quaternion, and use the rotation property, isn't that going to be equivalent to just a rotation around the z-axis?

Alternately, is there some better or simpler way to just get a vector or rotation indicating the sprites facing? (And all my sprites are aligned to face right).

Update: I tried this bound the the up arrow key, but it always moves me right.

 Vector2 facing = cMathFunctions.RotationToVector(transform.rotation.z * Mathf.Deg2Rad);
 transform.position += (new Vector3(facing.x, facing.y,0) * Time.deltaTime * Thrust);

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
Best Answer

Answer by calmcarrots · Aug 31, 2014 at 05:50 AM

http://docs.unity3d.com/ScriptReference/Transform-eulerAngles.html http://docs.unity3d.com/ScriptReference/Quaternion-eulerAngles.html http://answers.unity3d.com/questions/462073/how-to-use-euler-angles-to-rotate-an-object-.html http://en.wikibooks.org/wiki/Cg_Programming/Unity/Rotations

Comment
Add comment · Show 4 · 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 MadTigger · Aug 31, 2014 at 06:06 AM 0
Share

So it looks like you are suggestion that the best way of managing rotation is to store it locally, and then use that value to manipulate the transform object. That would work if you didn't use physics modeling. The problem is, because a collision with another object could impart a torque on the sprite/object, wouldn't that cause your local heading variable to get out of sync with the actual heading being displayed? I would think that you need to read heading off of something tied to the physics engine output to get a correct value.

avatar image calmcarrots · Aug 31, 2014 at 06:37 AM 0
Share

I have no idea what you are talking about. Euler angles just converts Quaternions to a Vector3. That way you can say

 transform.rotation = Quaternion.Euler(Vector3.down);

You can grab the rotation every second so that you can have it updated every frame.

 Vector3 curRot = transform.localEulerAngles;
 Vector3 curRot = transform.eulerAngles;
 Quaternion curRot = transform.localRotation;
 Quaternion curRot = transform.localRotation;

etc.

Then you can modify it like:

 transform.rotation = Quaternion.Euler(new Vector3(0f, 38f, 56f));
 transform.eulerAngles = new Vector3(0f, 38f, 56f);
 //Or like this
 Quaternion curRot = transform.rotation;
 curRot = Quaternion.Euler(new Vector3(0f, 38f, 56f));

Those are some examples of how to use a Quaternion with a transform. For your game, if you would like to see the rotation, you could try

 Debug.Log(transform.eulerAngles);
 
 Vector3 rightDir = new Vector3(0f, 90f, 0f);
 Vector3 upDir = Vector3.up;
 Vector3 leftDir = new Vector3(0f, 270f, 0f);
 Vector3 downDir = new Vector3(0f, 180f, 0f);
 Vector3 curRot = transform.eulerAngles;
 
 if (curRot == rightDir)
       Debug.Log(transform.name + " is facing right");
 else if (curRot == upDir)
       Debug.Log(transform.name + " is facing up");
 else if (curRot == leftDir)
       Debug.Log(transform.name + " is facing left");
 else if (curRot == downDir)
       Debug.Log(transform.name + " is facing down");

Try to play around with rotations to get used to them.

avatar image calmcarrots · Aug 31, 2014 at 06:41 AM 1
Share

I sorta understand what you are saying. If you collide with the object, the physics engine directly changes the object's rotation THROUGH the transform. It does not save the rotation in a separate value so if you collide with something, then the variable that you stored the rotation in will be up to date (as long as you update the variable every second).

avatar image MadTigger · Aug 31, 2014 at 07:58 AM 0
Share

After reading and playing with your suggestions, I came up with this:

 Vector2 facing = c$$anonymous$$athFunctions.RotationToVector(transform.eulerAngles.z * $$anonymous$$athf.Deg2Rad);
 transform.position += (new Vector3(facing.x, facing.y,0) * Time.deltaTime * Thrust);

...which worked very nicely to move a player along the current heading. Thanks for the info about the eulerAngles property.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Rotating a Sprite to face mouse / target? 2 Answers

UI Image has no sprite definition 0 Answers

Running 2D Sprite Animation Once? 2 Answers

Is it possible to turn off/suppress event calls in an animation at runtime? 2 Answers

What is the best way to add 1000 sprite-trees? 2 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