Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var moveDelay : float = 0.5;
- var gridSize = 1;
- private var x = 0;
- private var y = 0;
- private var moveX = 1;
- private var moveY = 0;
- private var allowChangeDirection = true;
- private var bodyPieceNum = 0;
- var snakeBody : ArrayList;
- var body : Transform;
- var head : Transform;
- function Start() {
- snakeBody = new ArrayList();
- bodies = Instantiate(body,head.transform.position,head.transform.rotation);
- snakeBody.Add(bodies);
- Move();
- }
- function Move() {
- while (true) {
- yield WaitForSeconds(moveDelay);
- // count the next body piece number (wrapping to zero):
- bodyPieceNum++;
- bodyPieceNum = (bodyPieceNum % snakeBody.length);
- // place the next 'body piece' at the old head position
- snakeBody[bodies].position = transform.position;
- // code here is executed once per 'move delay'.
- `
- x += moveX;
- y += moveY;
- transform.position = Vector3 ( x * gridSize, 0, y * gridSize);
- // now the snake has moved a step, we can allow the user to make
- // another change in direction:
- allowChangeDirection = true;
- }
- }
- function Update() {
- if(Input.GetKeyDown(KeyCode.D) && moveY != -1 && allowChangeDirection){
- moveX = 0;
- moveY = 1;
- allowChangeDirection = false;
- }
- if(Input.GetKeyDown(KeyCode.A) && moveY != 1 && allowChangeDirection){
- moveX = 0;
- moveY = -1;
- allowChangeDirection = false;
- }
- if(Input.GetKeyDown(KeyCode.S) && moveX != -1 && allowChangeDirection){
- moveX = 1;
- moveY = 0;
- allowChangeDirection = false;
- }
- if(Input.GetKeyDown(KeyCode.W) && moveX != 1 && allowChangeDirection){
- moveX = -1;
- moveY = 0;
- allowChangeDirection = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment