Advertisement
kasru

Movement

Jan 9th, 2013
20,794
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //****** Donations are greatly appreciated.  ******
  2. //****** You can donate directly to Jesse through paypal at  https://www.paypal.me/JEtzler   ******
  3.  
  4. private var jumpSpeed:float = 18.0;
  5. private var gravity:float = 32.0;
  6. private var runSpeed:float = 15.0;
  7. private var walkSpeed:float = 45.0;
  8. private var rotateSpeed:float = 150.0;
  9.  
  10. private var grounded:boolean = false;
  11. private var moveDirection:Vector3 = Vector3.zero;
  12. private var isWalking:boolean = false;
  13. private var moveStatus:String = "idle";
  14.  
  15. static var dead : boolean = false;
  16.  
  17. function Update ()
  18. {
  19.  
  20.     if(dead == false) {
  21.    
  22.    
  23.    
  24.  
  25.  
  26.     // Only allow movement and jumps while grounded
  27.     if(grounded) {
  28.         moveDirection = new Vector3((Input.GetMouseButton(1) ? Input.GetAxis("Horizontal") : 0),0,Input.GetAxis("Vertical"));
  29.        
  30.         // if moving forward and to the side at the same time, compensate for distance
  31.         // TODO: may be better way to do this?
  32.         if(Input.GetMouseButton(1) && Input.GetAxis("Horizontal") && Input.GetAxis("Vertical")) {
  33.             moveDirection *= .7;
  34.            
  35.         }
  36.        
  37.         moveDirection = transform.TransformDirection(moveDirection);
  38.         moveDirection *= isWalking ? walkSpeed : runSpeed;
  39.        
  40.         moveStatus = "idle";
  41.         if(moveDirection != Vector3.zero)
  42.             moveStatus = isWalking ? "walking" : "running";
  43.        
  44.         // Jump!
  45.         //if(Input.GetButton("Jump"))
  46.        
  47.         if (Input.GetKeyDown(KeyCode.Space))
  48.        
  49.             moveDirection.y = jumpSpeed;
  50.     }
  51.    
  52.     // Allow turning at anytime. Keep the character facing in the same direction as the Camera if the right mouse button is down.
  53.     if(Input.GetMouseButton(1)) {
  54.         transform.rotation = Quaternion.Euler(0,Camera.main.transform.eulerAngles.y,0);
  55.     } else {
  56.         transform.Rotate(0,Input.GetAxis("Horizontal") * rotateSpeed * Time.deltaTime, 0);
  57.        
  58.     }
  59.    
  60.    
  61.        
  62.        
  63.        
  64.    
  65.     // Toggle walking/running with the T key
  66.     //if(Input.GetKeyDown("t"))
  67.         //isWalking = !isWalking;
  68.  
  69.  
  70.    
  71.    
  72.     //Apply gravity
  73.     moveDirection.y -= gravity * Time.deltaTime;
  74.  
  75.    
  76.     //Move controller
  77.     var controller:CharacterController = GetComponent(CharacterController);
  78.     var flags = controller.Move(moveDirection * Time.deltaTime);
  79.     grounded = (flags & CollisionFlags.Below) != 0;
  80.    
  81.  
  82.     }
  83.    
  84.    
  85.     if(Input.GetMouseButton(1) || Input.GetMouseButton(0)) {
  86.         //Screen.lockCursor = true;
  87.        
  88.         Screen.showCursor = false;
  89.        
  90.        
  91.         //var mouse1 = Input.mousePosition.y;
  92.         //var mouse2 = Input.mousePosition.x;
  93.        
  94.         }
  95.        
  96.         //Vector3 mousePos = Input.mousePosition;
  97.     else  {
  98.         //Screen.lockCursor = false;
  99.         Screen.showCursor = false;
  100.        
  101.         //Input.mousePosition.y = mouse1;
  102.         //Input.mousePosition.x = mouse2;
  103.        
  104.         //Input.mousePosition = mousePos;
  105.        
  106.         }
  107.    
  108.    
  109. }
  110.  
  111. @script RequireComponent(CharacterController)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement