Advertisement
kasru

C# Rpg Movement

Jun 10th, 2015
6,513
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.93 KB | None | 0 0
  1. //****** Donations are greatly appreciated.  ******
  2. //****** You can donate directly to Jesse through paypal at  https://www.paypal.me/JEtzler   ******
  3.  
  4. using System.Collections;
  5.  
  6. public class UserMovement : MonoBehaviour {
  7.  
  8.     public float jumpSpeed = 30.0f;
  9.     public float gravity = 55.0f;
  10.     public float runSpeed = 70.0f;
  11.     public float runSpeed1 = 70.0f;
  12.     public float runSpeed2 = 140.0f;
  13.     private float walkSpeed = 90.0f;
  14.     private float rotateSpeed = 150.0f;
  15.    
  16.     public bool grounded;
  17.     private Vector3 moveDirection = Vector3.zero;
  18.     private bool isWalking;
  19.     private string moveStatus = "idle";
  20.  
  21.     public GameObject camera1;
  22.     public CharacterController controller;
  23.     public bool isJumping;
  24.     private float myAng = 0.0f;
  25.     public bool canJump = true;
  26.  
  27.     void Start () {
  28.  
  29.         controller = GetComponent<CharacterController>();
  30.     }
  31.    
  32.     void Update ()
  33.     {
  34.         //force controller down slope. Disable jumping
  35.         if(myAng > 50) {
  36.  
  37.             canJump = false;
  38.         }
  39.         else {
  40.  
  41.             canJump = true;
  42.         }
  43.  
  44.             if(grounded) {
  45.                
  46.                 isJumping = false;
  47.                
  48.                 if(camera1.transform.gameObject.transform.GetComponent<UserCamera>().inFirstPerson == true) {
  49.                     moveDirection = new Vector3((Input.GetMouseButton(0) ? Input.GetAxis("Horizontal") : 0),0,Input.GetAxis("Vertical"));
  50.                 }
  51.                 moveDirection = new Vector3((Input.GetMouseButton(1) ? Input.GetAxis("Horizontal") : 0),0,Input.GetAxis("Vertical"));
  52.                    
  53.                 moveDirection = transform.TransformDirection(moveDirection);
  54.                 moveDirection *= isWalking ? walkSpeed : runSpeed;
  55.                
  56.                 moveStatus = "idle";
  57.                
  58.                
  59.                
  60.                 if(moveDirection != Vector3.zero)
  61.                     moveStatus = isWalking ? "walking" : "running";
  62.                
  63.                 if (Input.GetKeyDown(KeyCode.Space) && canJump) {      
  64.                     moveDirection.y = jumpSpeed;
  65.                     isJumping = true;
  66.                 }
  67.                
  68.             }
  69.            
  70.            
  71.             // Allow turning at anytime. Keep the character facing in the same direction as the Camera if the right mouse button is down.
  72.            
  73.             if(camera1.transform.gameObject.transform.GetComponent<UserCamera>().inFirstPerson == false) {
  74.                 if(Input.GetMouseButton(1)) {
  75.                     transform.rotation = Quaternion.Euler(0,Camera.main.transform.eulerAngles.y,0);
  76.                 } else {
  77.                     transform.Rotate(0,Input.GetAxis("Horizontal") * rotateSpeed * Time.deltaTime, 0);
  78.                    
  79.                 }
  80.             }
  81.             else {
  82.                 if(Input.GetMouseButton(0) || Input.GetMouseButton(1)) {
  83.                     transform.rotation = Quaternion.Euler(0,Camera.main.transform.eulerAngles.y,0);
  84.                 }
  85.                
  86.             }
  87.            
  88.             //Apply gravity
  89.             moveDirection.y -= gravity * Time.deltaTime;
  90.            
  91.            
  92.             //Move controller
  93.             CollisionFlags flags;
  94.             if(isJumping) {
  95.                 flags = controller.Move(moveDirection * Time.deltaTime);
  96.             }
  97.             else {
  98.                 flags = controller.Move((moveDirection + new Vector3(0,-100,0)) * Time.deltaTime);
  99.             }
  100.            
  101.             grounded = (flags & CollisionFlags.Below) != 0;
  102.  
  103.     }
  104.  
  105.     void OnControllerColliderHit(ControllerColliderHit hit) {
  106.  
  107.             myAng = Vector3.Angle(Vector3.up, hit.normal);
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement