duck

Untitled

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