duck

Untitled

May 21st, 2010
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var moveDelay : float = 0.5;
  2. var gridSize = 1;
  3. private var x = 0;
  4. private var y = 0;
  5.  
  6. private var moveX = 1;
  7. private var moveY = 0;
  8.  
  9. private var allowChangeDirection = true;
  10.  
  11. var snakeBody : ArrayList;
  12. var body : Transform;
  13. var head : Transform;
  14.  
  15. function Start() {
  16.     snakeBody = new ArrayList();
  17.     bodies = Instantiate(body,head.transform.position,head.transform.rotation);
  18.     snakeBody.Add(bodies);
  19.     Move();
  20. }
  21.  
  22. function Move() {
  23.  
  24.     while (true) {
  25.         yield WaitForSeconds(moveDelay);
  26.    
  27.         // count the next body piece number (wrapping to zero):
  28.         bodyPieceNum++;
  29.         bodyPieceNum %= bodyPieces.length;
  30.        
  31.         // place the next 'body piece' at the old head position
  32.         bodyPieces[bodyPieceNum].position = transform.position;
  33.        
  34.         // code here is executed once per 'move delay'.
  35. `  
  36.         x += moveX;
  37.         y += moveY;
  38.    
  39.         transform.position = Vector3 ( x * gridSize, 0, y * gridSize);
  40.        
  41.         // now the snake has moved a step, we can allow the user to make
  42.         // another change in direction:
  43.         allowChangeDirection = true;
  44.        
  45.     }
  46. }
  47.  
  48. function Update() {
  49.  
  50.     if(Input.GetKeyDown(KeyCode.D) && moveY != -1 && allowChangeDirection){
  51.         moveX = 0;
  52.         moveY = 1;
  53.         allowChangeDirection = false;
  54.     }
  55.    
  56.     if(Input.GetKeyDown(KeyCode.A) && moveY != 1 && allowChangeDirection){
  57.         moveX = 0;
  58.         moveY = -1;
  59.         allowChangeDirection = false;
  60.     }
  61.            
  62.     if(Input.GetKeyDown(KeyCode.S) && moveX != -1 && allowChangeDirection){
  63.         moveX = 1;
  64.         moveY = 0;
  65.         allowChangeDirection = false;
  66.     }
  67.    
  68.     if(Input.GetKeyDown(KeyCode.W) && moveX != 1 && allowChangeDirection){
  69.         moveX = -1;
  70.         moveY = 0;
  71.         allowChangeDirection = false;
  72.     }
  73.    
  74. }
Advertisement
Add Comment
Please, Sign In to add comment