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 DawdleDev · May 01, 2018 at 02:53 AM · 2drotationtransformrotatefollow

[Help] Object flips around when using transform.Rotate

I am using a chain of objects that all follow each other in sequence. It behaves similarly to a snake. The game is done in 2D. The issue though is that when rotating each object to face the next, the objects flip around at the transition between 180 and -180. Does anyone know what the issue could be? I am using this to make them face each other smoothly to create a snake-joint effect:

 transform.Rotate ((FollowTarget.transform.rotation.eulerAngles - transform.rotation.eulerAngles) / 25);

Any help is much appreciated. I tried looking it up, and found this page: https://answers.unity.com/questions/313187/how-to-prevent-transformrotate-from-oddly-flipping.html Their problem matches the issue I am having precisely; however, it is useless to me as the page and answer provides no means of solving it.

EDIT: Correction, this is happening between the transition from 0 to 360 degrees, not -180 to 180. Shouldn't make a difference.

EDIT: I know what is happening. When the head makes the movement from 0 to 360 degrees, the segments get the results as if the head had rotated a full 360 in the other direction and attempt to follow it. However, I have no idea how to solve it. I tried multiple solutions involving rotating the segments with the head, but nothing worked. The only way for me to fix this is to make the segments stretch out in a straight line to counter the effects of the head, but this is just as bad and terrible looking as the problem it is trying to solve.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Scribe · May 07, 2018 at 02:48 PM

Well you can fix it in a number of ways (as always).

If you really want to keep using Eulerian angle representation then you could use Mathf.LerpAngle or Mathf.MoveTowardsAngle. These functions take into account that 360 and 0 are the same value, and will go the shortest distance taking that into account, e.g. MoveTowardsAngle(350, 0, 5) should equal 355.

If you are happy to move to using Quaternions (which are generally better for avoiding this kind of issue once you get your head around them) you can use similar functions:

 transform.rotation = Quaternion.RotateTowards(transform.rotation, FollowTarget.transform.rotation, 5 * Time.deltaTime);

or to recreate the smoothing of your current solution you could try:

 transform.rotation = Quaternion.Lerp(transform.rotation, FollowTarget.transform.rotation, 0.5f);

The 0.5f value means that it will always halve the distance between one rotation and the other, meaning it will appear to slow down in 'rate' of rotation as it gets close to the desired point.

As a completely different solution, you could make each segment LookAt the segment in front of it, which might be the easiest!

 transform.LookAt(FollowTarget.transform.position);
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 DawdleDev · May 07, 2018 at 03:39 PM 1
Share

Thank you! transform.rotation = Quaternion.Lerp(transform.rotation, FollowTarget.transform.rotation, 0.04f); worked perfectly!

avatar image
-1

Answer by NoDumbQuestion · May 01, 2018 at 02:45 PM

@notpresident35 You are rotating object in local space. Sometime local rotate funny because of parent.

Read the document rotate in World space might help(remember there are 3 axis in eulerAngles).

Here is another solution of flipping object just by set local axis scale to -1.

Comment
Add comment · Show 3 · 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 DawdleDev · May 01, 2018 at 03:52 PM 0
Share

@TheWorstUsername Space.World did nothing. All of the separate objects are not parented to anything or each other, so this makes no difference. It might have been a little unclear what I was talking about. Here's what is happening:

alt text

flipping.gif (371.0 kB)
avatar image NoDumbQuestion · May 01, 2018 at 04:03 PM 0
Share

@notpresident35 look like something wrong with your code when collision happen than the transform rotate itself.

Or your image pivot texture import settings is not in center.

avatar image DawdleDev NoDumbQuestion · May 01, 2018 at 04:12 PM 0
Share

The pivot on all textures is center. There are no colliders on any of the objects. This specifically happens when the head turns so that it faces mostly in the other direction.

avatar image
-1

Answer by Deeblock · May 01, 2018 at 04:06 PM

Not really certain but you could test it out - calling transform.rotate rotates the object around all three axis. Perhaps you might want to lock its rotation around the x and y axis only if you're working in 2D space?

Comment
Add comment · Show 3 · 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 DawdleDev · May 01, 2018 at 04:12 PM 0
Share

No objects are being rotated on anything but the z axis. I exited out of 2D mode and tested. Nothing is rotating 3 dimensionally.

avatar image Deeblock DawdleDev · May 01, 2018 at 04:15 PM 0
Share

You could use Debug.Log to view the angles at which a specific block is rotating at as you move to figure out what is going wrong. Probably something to do with how you are calculating the rotation.

avatar image DawdleDev · May 01, 2018 at 04:17 PM 0
Share

Okay, I've figured out some things. What's happening is that when the head rotates from 0 to 360 or vice versa, the segments believe that it has simply rotated all the way around in the other direction, and rotate around in that direction. It only is obvious because of the smooth rotation that I am using. How do I prevent this?

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

205 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

Related Questions

Camera rotation around player while following. 6 Answers

Find left joystick Vector2 away from sprite's up direction. 1 Answer

Rotating a 2D GameObject on Z regardless of the distance. 2 Answers

How can I rotate my player to look at the direction my Joystick is pointing to? (Top-Down 2D) 3 Answers

Following another object's position/rotation like parent/child relationship? 4 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