Advertisement
Guest User

character_controller unity script

a guest
Aug 13th, 2014
608
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.98 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class character_controller : MonoBehaviour {
  5.    
  6.     public float MoveSpeed = 0.2f;
  7.    
  8.     public bool isGrounded = false;
  9.     public bool facingRight = true;
  10.     public bool canMoveRight = true;
  11.     public bool canMoveLeft = true;
  12.     public bool canJump = true;
  13.     private bool isJumpMoveStoped = false;
  14.     private bool isWalkingSoundPlaying = false;
  15.     private bool isStuck = false;
  16.     private Vector3 lastPosition;
  17.     private int timerforstuck = 0;
  18.    
  19.     public Vector3 MoveVector;
  20.     private Quaternion FacingVector;
  21.  
  22.     public Vector2 jumpForce = new Vector2(0, 300);
  23.  
  24.     void Update () {
  25.         checkMovement();
  26.         checkFacing();
  27.         checkStuck();
  28.         playSound ();
  29.     }
  30.    
  31.     void checkMovement() {
  32.         if (Input.GetKeyDown(KeyCode.Space)) jump();
  33.         if ((Input.GetAxis("Horizontal") < -0.01 || Input.GetAxis("Horizontal") > 0.01) && isGrounded) move();
  34.  
  35.         if (!isGrounded)
  36.         {
  37.             if (jumpForce.x != 0 && (!canMoveLeft || !canMoveRight)) {
  38.                 isJumpMoveStoped = true;
  39.             } else if (isJumpMoveStoped){
  40.                 isJumpMoveStoped = false;
  41.                 rigidbody2D.AddForce(new Vector2(50f, 0f));
  42.             }
  43.         }
  44.     }
  45.    
  46.     void move() {
  47.         if (isGrounded) {
  48.             // Перемещение персонажа вправо-влево
  49.             if (Input.GetAxis ("Horizontal") > 0.01 && canMoveRight) {
  50.                 MoveVector.x = Input.GetAxis ("Horizontal") * MoveSpeed;
  51.             } else if (Input.GetAxis ("Horizontal") < 0.01 && canMoveLeft) {
  52.                 MoveVector.x = Input.GetAxis ("Horizontal") * MoveSpeed;
  53.             } else {
  54.                 MoveVector.x = 0;
  55.             }
  56.  
  57.             // Направление спрайта персонажа
  58.             if (Input.GetAxis ("Horizontal") > 0.005) facingRight = true;
  59.             if (Input.GetAxis ("Horizontal") < -0.005) facingRight = false;
  60.         }
  61.         transform.position += MoveVector;
  62.     }
  63.    
  64.     void jump() {
  65.         if(isGrounded && canJump) {
  66.             rigidbody2D.velocity = Vector2.zero;
  67.             jumpForce.x = (facingRight) ? 150f:-150f;
  68.             rigidbody2D.AddForce(jumpForce);
  69.             isGrounded = false;
  70.         } else if (!isGrounded && canJump) {
  71.             jumpForce.x = 0;
  72.         }
  73.     }
  74.    
  75.     void checkFacing() {
  76.         FacingVector.y = (facingRight) ? 0 : 180;
  77.         transform.rotation = FacingVector;
  78.     }
  79.  
  80.     void checkStuck()
  81.     {
  82.         timerforstuck++;
  83.         if (timerforstuck == 15)
  84.         {
  85.             if (isStuck)
  86.             {
  87.                 Debug.Log(timerforstuck);
  88.                 isStuck = false;
  89.                 rigidbody2D.fixedAngle = true;
  90.             }
  91.             if (!isGrounded && transform.position == lastPosition)
  92.             {
  93.                 Debug.Log("ololo");
  94.                 rigidbody2D.fixedAngle = false;
  95.                 isStuck = true;
  96.             }
  97.             lastPosition = transform.position;
  98.             timerforstuck = 0;
  99.         }
  100.     }
  101.  
  102.     void playSound() {
  103.         if (isGrounded) {
  104.             // Проигрывание звуков
  105.             if ((Input.GetAxis ("Horizontal") > 0.005 ||  Input.GetAxis ("Horizontal") < -0.005) && !isWalkingSoundPlaying)
  106.             {
  107.                 isWalkingSoundPlaying = true;
  108.                 audio.Play();
  109.             }
  110.             if (Input.GetAxis ("Horizontal") < 0.005 &&  Input.GetAxis ("Horizontal") > -0.005) {
  111.                 isWalkingSoundPlaying = false;
  112.                 audio.Stop();
  113.             }
  114.         } else audio.Stop ();
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement