duck

Untitled

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