Advertisement
sphinx2001

FPC

Mar 7th, 2020
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.66 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. [RequireComponent(typeof(Rigidbody))]
  5.  
  6. public class FPC : MonoBehaviour
  7. {
  8.  
  9.     public float speed = 1.5f;
  10.  
  11.     public Transform head;
  12.  
  13.     public float sensitivity = 5f; // чувствительность мыши
  14.     public float headMinY = -40f; // ограничение угла для головы
  15.     public float headMaxY = 40f;
  16.  
  17.     public KeyCode jumpButton = KeyCode.Space; // клавиша для прыжка
  18.     public float jumpForce = 10; // сила прыжка
  19.     public float jumpDistance = 1.2f; // расстояние от центра объекта, до поверхности
  20.  
  21.     private Vector3 direction;
  22.     private float h, v;
  23.     private int layerMask;
  24.     private Rigidbody body;
  25.     private float rotationY;
  26.  
  27.     void Start()
  28.     {
  29.         body = GetComponent<Rigidbody>();
  30.         body.freezeRotation = true;
  31.         layerMask = 1 << gameObject.layer | 1 << 2;
  32.         layerMask = ~layerMask;
  33.     }
  34.  
  35.     void FixedUpdate()
  36.     {
  37.         body.AddForce(direction * speed, ForceMode.VelocityChange);
  38.  
  39.         // Ограничение скорости, иначе объект будет постоянно ускоряться
  40.         if (Mathf.Abs(body.velocity.x) > speed)
  41.         {
  42.             body.velocity = new Vector3(Mathf.Sign(body.velocity.x) * speed, body.velocity.y, body.velocity.z);
  43.         }
  44.         if (Mathf.Abs(body.velocity.z) > speed)
  45.         {
  46.             body.velocity = new Vector3(body.velocity.x, body.velocity.y, Mathf.Sign(body.velocity.z) * speed);
  47.         }
  48.     }
  49.  
  50.     bool GetJump() // проверяем, есть ли коллайдер под ногами
  51.     {
  52.         RaycastHit hit;
  53.         Ray ray = new Ray(transform.position, Vector3.down);
  54.         if (Physics.Raycast(ray, out hit, jumpDistance, layerMask))
  55.         {
  56.             return true;
  57.         }
  58.  
  59.         return false;
  60.     }
  61.  
  62.     void Update()
  63.     {
  64.         h = Input.GetAxis("Horizontal");
  65.         v = Input.GetAxis("Vertical");
  66.  
  67.         // управление головой (камерой)
  68.         float rotationX = head.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivity;
  69.         rotationY += Input.GetAxis("Mouse Y") * sensitivity;
  70.         rotationY = Mathf.Clamp(rotationY, headMinY, headMaxY);
  71.         head.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
  72.  
  73.         // вектор направления движения
  74.         direction = new Vector3(h, 0, v);
  75.         direction = head.TransformDirection(direction);
  76.         direction = new Vector3(direction.x, 0, direction.z);
  77.  
  78.         if (Input.GetKeyDown(jumpButton) && GetJump())
  79.         {
  80.             body.velocity = new Vector2(0, jumpForce);
  81.         }
  82.     }
  83.  
  84.     void OnDrawGizmosSelected() // подсветка, для визуальной настройки jumpDistance
  85.     {
  86.         Gizmos.color = Color.red;
  87.         Gizmos.DrawRay(transform.position, Vector3.down * jumpDistance);
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement