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 getafix99 · Jan 29, 2013 at 09:53 PM · movementsnake

3D snake classical movement

this is my code for the movement

 if (Time.time - lastUpdate >=snakeSpeed)
 {
 var i : GameObject;
 var k : GameObject;
 
 for (var j=snakeLength;j>1;j--)
 {
 i = GameObject.Find("snakeBody"+j);
 k = GameObject.Find("snakeBody"+(j-1));
 i.transform.position = k.transform.position;
 }
 i = GameObject.Find("snakeBody1");
 i.transform.position = transform.position;
 
 
 if (right == true) {transform.Rotate(Vector3(0,-90,0)); right = false;}
 if (left == true) {transform.Rotate(Vector3(0,90,0)); left = false;}
 transform.Translate(Vector3(0,0,1));
 
 
 lastUpdate = Time.time;
 }

as you can see it's really simple, all parts are cubes of size 1,1,1, each time frame (determined by snakeSpeed var) the procedure move the last snake piece to the position of the snake piece that is ahead, then the last body get the position of the snakeHead (which the script attached to), and then the movement of the snakeHead is performed and it works

the problem is even if I make the time (snakeSpeed) to be 0.05 it works by 20 frames for second so it doesn't look flowish, I need help to convert this code to work by frames and not time, if I could make

transform.Translate(Vector3(0,0,0.166)); instead transform.Translate(Vector3(0,0,1)); and make all pieces follow accordingly it will be ideal for me

please share any thoughts and help I will appreciate it a lot and thank you

Comment
Add comment · Show 1
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 Fattie · Jan 29, 2013 at 10:15 PM 0
Share

i think you need to learn about Invoke, InvokeRepeating, FixedUpdate and some others. try unityGE$$anonymous$$S.com for many articles

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by robertbu · Jan 30, 2013 at 02:01 AM

Typically turning and rotation use Time.deltaTime, so that movements are consistent no matter what the frame rate. Here is an example. Attach it to a block and use the arrow keys to drive it around the screen. Is this the motion of the head you are looking for?

 public class SnakeHead : MonoBehaviour {
     private float fTurnRate = 90.0f;  // 90 degrees of turning per second
     private float fSpeed = 1.0f;  // Units per second of movement;
     
     void Update () {
         if (Input.GetKey (KeyCode.LeftArrow))
             transform.Rotate (Vector3.forward * fTurnRate * Time.deltaTime);
         if (Input.GetKey (KeyCode.RightArrow))
             transform.Rotate (-Vector3.forward * fTurnRate * Time.deltaTime);
         transform.localPosition = transform.localPosition + transform.up * fSpeed * Time.deltaTime;
     }
 }

Here is a second script for testing the body. Attach it to a group of sphere and spread the spears out a bit. In the inspector attach one to the "head" and then attach each sphere to another so they are chained with each one having a goLeader..

 public class SnakeBody : MonoBehaviour {
     public GameObject goLeader = null;
 
     void Update () {
         if (goLeader == null) return;
         Vector3 v3FromLeader =  transform.position - goLeader.transform.position;
         v3FromLeader = v3FromLeader.normalized * transform.localScale.y;  
         transform.position = v3FromLeader + goLeader.transform.position;
     }
 }
Comment
Add comment · Show 2 · 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 getafix99 · Jan 30, 2013 at 01:52 PM 0
Share

thank you! I will look into it and tell you how it goes

avatar image haim96 · Apr 28, 2014 at 06:57 PM 0
Share

this is a bit old but defiantly helpful! i would close this thread and select this as answer! thanks!

avatar image
0

Answer by Adam-Buckner · Jan 29, 2013 at 10:07 PM

If it were me, I would do a search on the subject of "snake" using the Google Custom Search Engine that searches everything Unity. I am sure this question has been asked and answered a few times. I am sure that this has been solved in a number of different ways, and if you get a selection of solutions, you can pick the one that works best for you.

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 getafix99 · Jan 29, 2013 at 10:31 PM 0
Share

thank you very much, I already searched but not with this engine, so I will search some more.

meanwhile I would like to show you what movement I'm looking for something like this -> snake game

this is the movement I currently use, every time a snake piece is added I'm adding a SmoothFollow component to it -> snake movement video 1

this is the movement from my OP -> snake movement video 2

my Ideal movement is to have 360 rotation, adjustable speed, and all pieces have to go through the route provided by the snake head like the classical game (like video 2) so I'm looking for something in between both, any idea for it?

also I have another question, I wanna make all cube object act as the bones of the snake and after I calculate the movement I would like to skin them with a big snake object/model that will get it's shape by it's 'bones' objects, what's my approach for this?

avatar image Adam-Buckner ♦♦ · Jan 30, 2013 at 12:22 AM 0
Share

I have converted your "answer" to a "comment". (^_^) Remember, Answers is different from the forum, so comments should be added to the proposed Answer or to the Original Question unless they are an answer.

avatar image Adam-Buckner ♦♦ · Jan 30, 2013 at 12:26 AM 0
Share

Your video of the OP code is very much like the classic snake, what is it that you don't like about it that you get from the snake game video?

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

13 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

Related Questions

Make an object face forward in a snake game. 2 Answers

Making a bubble level (not a game but work tool) 1 Answer

Unity C# - Snake Movment 1 Answer

I want my snake to move continuosly if i press...up it should move up until i press another button....like the original snake game 2 Answers

slither snake game 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