Advertisement
Guest User

Third Person Controller (Unity)

a guest
Oct 16th, 2013
865
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.30 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class ThirdPersonCharaController : MonoBehaviour {
  5.    
  6.     // Objects
  7.     public GameObject PlayerHolder;
  8.    
  9.     // Animation Details
  10.     public AnimationClip idleAnimation;
  11.     public AnimationClip walkAnimation;
  12.     public float walkMaxAnimationSpeed = 0.75f;
  13.    
  14.     // the movement Details
  15.     public float gravity = 20.0f;
  16.     public float moveSpeed = 10.0f;
  17.     public float RotateSpeed = 0.1f;
  18.     public bool LockMovement = false;
  19.    
  20.    
  21.     // Private Fields
  22.     private Animation _animation;
  23.     private CharacterController controller;
  24.     private Vector3 moveDirection = Vector3.zero;
  25.     private Vector3 moveVelocity;
  26.     private float oldgravity;
  27.     private float oldRotateSpeed;
  28.     private float new_direction = 0.0f;
  29.     private float rotate_t = 0.0f;
  30.    
  31.    
  32.     //=====================================================================
  33.     // Use this for initialization
  34.     void Start () {
  35.        
  36.         oldRotateSpeed = RotateSpeed;
  37.        
  38.         oldgravity = gravity;
  39.         _animation = PlayerHolder.GetComponent<Animation>();
  40.         controller = transform.gameObject.GetComponent<CharacterController>();
  41.        
  42.         if(!_animation)
  43.             Debug.Log("The character you would like to control doesn't have animations. Moving her might look weird.");
  44.         if(!idleAnimation) {
  45.             _animation = null;
  46.             Debug.Log("No idle animation found. Turning off animations.");
  47.         }
  48.         if(!walkAnimation) {
  49.             _animation = null;
  50.             Debug.Log("No walk animation found. Turning off animations.");
  51.         }
  52.            
  53.     }
  54.    
  55.     //=====================================================================
  56.     // Updated every Frame
  57.     void Update() {
  58.         if(_animation != null){
  59.             _animation[walkAnimation.name].speed = walkMaxAnimationSpeed;
  60.             _animation.CrossFade(walkAnimation.name);
  61.         }
  62.        
  63.         if(controller != null && LockMovement == false){
  64.             if (controller.isGrounded) { // Gelandet
  65.                 Debug.Log("gelandet");
  66.                 moveVelocity = new Vector3(0,0,0);
  67.                 if(Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D)) // Rechts
  68.                 {
  69.                     if(Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W)){ // Für Diagonale
  70.                         rotate_t = 0.0f;
  71.                         new_direction = 45;
  72.                         moveVelocity = new Vector3(1,0,1);
  73.                     }
  74.                     else if(Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S)){
  75.                         rotate_t = 0.0f;
  76.                         new_direction = 135;
  77.                         moveVelocity = new Vector3(1,0,-1);
  78.                     }
  79.                     else{
  80.                         rotate_t = 0.0f;
  81.                         new_direction = 90;
  82.                         moveVelocity = new Vector3(1,0,0);
  83.                     }
  84.                 }
  85.                 else if(Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A)) // Links
  86.                 {
  87.                     if(Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W)){ // Für Diagonale
  88.                         rotate_t = 0.0f;
  89.                         new_direction = 315;
  90.                         moveVelocity = new Vector3(-1,0,1);
  91.                     }
  92.                     else if(Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S)){
  93.                         rotate_t = 0.0f;
  94.                         new_direction = 225;
  95.                         moveVelocity = new Vector3(-1,0,-1);
  96.                     }
  97.                     else{
  98.                         rotate_t = 0.0f;
  99.                         new_direction = 270;
  100.                         moveVelocity = new Vector3(-1,0,0);
  101.                     }
  102.                 }
  103.                 else if(Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W)) // Hoch
  104.                 {
  105.                     if(Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A)){ // Für Diagonale
  106.                         rotate_t = 0.0f;
  107.                         new_direction = 315;
  108.                         moveVelocity = new Vector3(-1,0,1);
  109.                     }
  110.                     else if(Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D)){
  111.                         rotate_t = 0.0f;
  112.                         new_direction = 45;
  113.                         moveVelocity = new Vector3(1,0,1);
  114.                     }
  115.                     else{
  116.                         rotate_t = 0.0f;
  117.                         new_direction = 0;
  118.                         moveVelocity = new Vector3(0,0,1);
  119.                     }
  120.                 }
  121.                 else if(Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S)) // Runter
  122.                 {
  123.                     if(Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A)){ // Für Diagonale
  124.                         rotate_t = 0.0f;
  125.                         new_direction = 255;
  126.                         moveVelocity = new Vector3(-1,0,-1);
  127.                     }
  128.                     else if(Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D)){
  129.                         rotate_t = 0.0f;
  130.                         new_direction = 135;
  131.                         moveVelocity = new Vector3(1,0,-1);
  132.                     }
  133.                     else{
  134.                         rotate_t = 0.0f;
  135.                         new_direction = 180;
  136.                         moveVelocity = new Vector3(0,0,-1);
  137.                     }
  138.                 }
  139.                
  140.                 moveDirection = moveVelocity;
  141.                 moveDirection = transform.TransformDirection(moveDirection);
  142.                 moveDirection *= moveSpeed;
  143.                 gravity = oldgravity;
  144.             }
  145.             else{ // FÄLLT NOCH
  146.                 Debug.Log("fällt mit " + gravity);
  147.                 gravity += 5.0f;
  148.                 gravity = Mathf.Clamp(gravity, oldgravity, oldgravity*2);
  149.             }
  150.            
  151.             Rotate_to();
  152.             if(Mathf.Abs(PlayerHolder.transform.eulerAngles.y-new_direction) < 35){
  153.                 moveDirection.y -= gravity * Time.deltaTime;
  154.                 controller.Move(moveDirection * Time.deltaTime);
  155.             }
  156.            
  157.         }
  158.     }
  159.    
  160.    
  161.     //=====================================================================
  162.     float f;
  163.     void Rotate_to(){
  164.         rotate_t += Time.deltaTime * RotateSpeed;
  165.         f = PlayerHolder.transform.eulerAngles.y;
  166.         if (f > 180.0f){
  167.             f -= 360.0f;
  168.         }
  169.         PlayerHolder.transform.eulerAngles = new Vector3(0, Mathf.Lerp(f, new_direction, rotate_t), 0);
  170.     }
  171.    
  172. //  Vector3 myRotation = Vector3.zero;
  173. //  void Rotate_to(){
  174. //      rotate_t += Time.deltaTime * RotateSpeed;
  175. //      myRotation.y = Mathf.Lerp(myRotation.y, new_direction, rotate_t);
  176. //      PlayerHolder.transform.eulerAngles = myRotation;
  177. //  }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement