• 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 zero_null · Mar 27, 2014 at 01:46 PM · rotationspheretranslation

How to rotate snake at an exact 90 degrees

I am making a snake game. I want to rotate my snake exact 90 degrees in the direction specified by swiping on the screen.(It's an android game). In t$$anonymous$$s I gained a bit success, can someone provide me with the mathematical formula to rotate snake by determining it's current head direction. As it rotates at 90 degrees correctly but not always in the direction specified by swipealt text

111.jpg (68.0 kB)
Comment
Add comment · Show 5
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 Leuthil · Mar 27, 2014 at 01:56 PM 0
Share

You will have to do some sort of swipe gesture detection so once an "upwards" swipe is detected that it should move up instead of wherever it was currently heading and likewise for all other directions. You will have to look into Input Touches to do this. It's not an easy task but not impossible. There are many assets on the Asset Store that do touch gesture recognition for this reason.

Since your snake is just spheres you can just change the movement direction instead of doing a real rotation.

Depending on how you want the snake movement it would change how to implement it as well. Do you want the body parts behind the head to move towards the head all the time (even when it changes direction), or do you want the body parts to take the exact full path that the head did?

avatar image zero_null · Mar 27, 2014 at 02:01 PM 0
Share

Leuthil well thanks for the rapid reply. I had enabled the swipe gestures, and you can see snake moves pretty fine. but the problem is that. when i swipe up and snake's head direction is on right-it moves towards down instead of upward :(

avatar image Leuthil · Mar 27, 2014 at 02:04 PM 0
Share

You will have to post the code you are using to change the direction of the snake otherwise there's really no way anyone can help :).

avatar image zero_null · Mar 27, 2014 at 02:13 PM 0
Share

My Update method snippet : kindly help me please

 void Update()
     {
         transform.Translate(new Vector3(0, 0, 0.09f));
 
         foreach (Touch T in Input.touches)
         {
             var P = T.position;
             if (T.phase == TouchPhase.Began && SwipeID == -1)
             {
                 SwipeID = T.fingerId;
                 StartPos = P;
             }
             else if (T.fingerId == SwipeID)
             {
                 Vector2 delta = P - StartPos;
                 if (T.phase == TouchPhase.Moved && delta.magnitude > minMovement)
                 {
                     SwipeID = -1;
                     if (Mathf.Abs(delta.x) > Mathf.Abs(delta.y))
                     {
                         if (delta.x > 0)
                         {
                                 transform.Rotate(new Vector3(0, 90, 0));
                             //    Debug.Log("Swipe Right Found");
                         }
                         else
                         {
                             transform.Rotate(new Vector3(0, -90, 0));
                           //  Debug.Log("Swipe Left Found");
                             
                         }
                     }
                     else
                     {
 
                         if (delta.y > 0)
                         {
                             transform.Rotate(new Vector3(0, 90, 0));
                             //Debug.Log("Swipe Up Found");
                         }
                         else
                         {
                             transform.Rotate(new Vector3(0, -90, 0));
                             //Debug.Log("Swipe Down Found");
                         }
 
                     }
                 }
                 else if (T.phase == TouchPhase.Canceled || T.phase == TouchPhase.Ended)
                     SwipeID = -1;
             }
         }   
         if (Time.time % 10 < 0.05)
             placeFood();
 
     }
 }
avatar image raza124 · Nov 25, 2016 at 06:38 AM 0
Share

hey @ad_adnan can you give your script?

1 Reply

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

Answer by Leuthil · Mar 27, 2014 at 02:37 PM

You cannot use transform.Rotate because that will always rotate relative to w$$anonymous$$chever direction is currently facing. What you want to use instead is Transform.eulerAngles w$$anonymous$$ch will explicitly set a rotation despite whatever the current rotation is:

 transform.eulerAngles = new Vector3(0f, yAngleRotation, 0f);

Each swipe direction would have a different yAngleRotation value (I'm not sure exactly w$$anonymous$$ch as it depends on what axis and direction your game is on / displayed at). These values would probably each be 0f, 90f, 180f, 270f.

One key t$$anonymous$$ng to note is to make sure you don't just set one axis rotation by itself or you will get bad results... as in don't do t$$anonymous$$s:

 // t$$anonymous$$s is very very bad, don't do t$$anonymous$$s!!!
 transform.eulerAngles.y = yAngleRotation;


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 zero_null · Mar 28, 2014 at 06:26 AM 0
Share

@Leuthil How I can say thanks to you? You solved my problem. Thanks allot. I am really very very grateful. Thanks Sir. Thanks allot .

avatar image Destran · Mar 28, 2014 at 07:53 AM 0
Share

Make sure to mark his answer as correct :D

avatar image zero_null · Apr 01, 2014 at 01:51 PM 0
Share

I think I can't vote this answer up. as I don't have that much reputation. @Destran

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

22 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

Related Questions

How to move an object a fixed distance along one of its axis 1 Answer

Infinite Raycast? 1 Answer

Setting object Rotation to RayCast line 1 Answer

Rotate object to face up from sphere 1 Answer

Transformation matrix in unity 0 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