- Home /
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
i think you need to learn about Invoke, InvokeRepeating, FixedUpdate and some others. try unityGE$$anonymous$$S.com for many articles
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;
}
}
thank you! I will look into it and tell you how it goes
this is a bit old but defiantly helpful! i would close this thread and select this as answer! thanks!
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.
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?
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.
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
![](https://koobas.hobune.stream/wayback/20220613091813im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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
slither snake game 0 Answers