Advertisement
sadravin

SADRavins Code Movement

Feb 17th, 2012
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.25 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Controls : MonoBehaviour
  5. {  
  6.  
  7.     public PackedSprite playerSprite; // assign your sprite to this in Unity  
  8.     public float speed = 6.0F;
  9.     //  public float jumpSpeed = 8.0F;
  10.     public float gravity = 20.0F;  
  11.     private Vector3 moveDirection = Vector3.zero;
  12.  
  13.  
  14.     void Update()
  15.     {  
  16.    
  17.         CharacterController controller = GetComponent<CharacterController>();
  18.    
  19.            
  20.         if (controller.isGrounded)
  21.         {
  22.             moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
  23.             moveDirection = transform.TransformDirection(moveDirection);
  24.             moveDirection *= speed;
  25.            
  26.            
  27.         //  if (Input.GetButton("Jump"))
  28.         //        moveDirection.y = jumpSpeed;
  29.            
  30.         }
  31.         moveDirection.y -= gravity * Time.deltaTime;
  32.         controller.Move(moveDirection * Time.deltaTime);
  33.         DetermineCurrentMoveDirection();
  34.         }
  35.        
  36.         public void DetermineCurrentMoveDirection()
  37.         {
  38.  
  39.  
  40.             var forward = false;
  41.             var backward = false;
  42.             var left = false;
  43.             var right = false;
  44.  
  45.  
  46.  
  47.         // move right  
  48.             if (moveDirection.x < 0.01f)
  49.         {
  50.             right = true;  
  51.         }  
  52.  
  53.         // move left  
  54.         else if(moveDirection.x > -0.01f)
  55.         {
  56.             left = true;
  57.         }  
  58.        
  59.         // move up  
  60.         else if(moveDirection.z > 0.01f)
  61.         {
  62.             forward = true;
  63.         }  
  64.  
  65.         // move down  
  66.         else if(moveDirection.z < -0.01f)
  67.         {
  68.             backward = true;
  69.         }
  70.         else
  71.             {
  72.             forward = false;
  73.             backward = false;
  74.             left = false;
  75.             right = false;
  76.             }
  77.  
  78.  
  79.         // move right
  80.         if (right == true)
  81.         {
  82.             playerSprite.PlayAnim(3);
  83.         }
  84.  
  85.         // move left  
  86.         if (left == true)
  87.         {
  88.             playerSprite.PlayAnim(2);
  89.         }
  90.  
  91.         // move up  
  92.         if (forward == true)
  93.         {
  94.             playerSprite.PlayAnim(0);
  95.         }
  96.  
  97.         // move down  
  98.         if (backward == true)
  99.         {
  100.             playerSprite.PlayAnim(1);
  101.         }
  102.     }
  103.      
  104.              
  105.      
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement