Advertisement
Guest User

zzz

a guest
Nov 15th, 2019
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.05 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerControl_CharacterController : MonoBehaviour //Реализация перемещения с помощью CharacterController
  6. {
  7.     public CharacterController PlayerController;
  8.     public float speed = 7; //Рекомендуемое значение 7f
  9.  
  10.     public float gravity = -9.8f; //Рекомендуемое значение -9.8f
  11.     public float Jump = 6.5f; //Рекомендуемое значение 6.5f
  12.     float jumpForce;
  13.     public float crouchSize = 0.8f; //Рекомендуемое значение 0.8f
  14.  
  15.     public float x;
  16.     public float z;
  17.  
  18.     public float reservX;
  19.     public float reservZ;
  20.  
  21.     public Transform groundCheck;
  22.     public float groundDistance = 0.4f;
  23.     public LayerMask triggerMask;
  24.     public LayerMask groundMask;
  25.     public bool isGrounded;
  26.     public bool inTrigger;
  27.     public bool movingOn;
  28.     public bool shiftOn;
  29.  
  30.     public float rayDistance; //Рекомендуемое значение 1.2f
  31.  
  32.     public float pushPower = 5;
  33.  
  34.     public bool noclipOn = false;
  35.  
  36.     public GameObject spawnPoint;
  37.     public GameObject PlayerCamera;
  38.     public Vector3 spawnPointInfo;
  39.  
  40.     Vector3 velocity;
  41.  
  42.     void Start()
  43.     {
  44.  
  45.     }
  46.  
  47.     public void Update()
  48.     {
  49.         //isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
  50.         //inTrigger = Physics.CheckSphere(groundCheck.position, groundDistance, triggerMask);
  51.         Cursor.visible = false;
  52.         Moving();
  53.  
  54.         SmoothFall();
  55.         Teleportation();
  56.         Crouch();
  57.         Noclip();
  58.         AlternativeCheckGround();
  59.         Jumping();
  60.     }
  61.  
  62.     void inAirSpeed()
  63.     {
  64.         //Перемещение во время полета
  65.     }
  66.     void Moving()
  67.     {
  68.         Vector3 move = transform.right * x + transform.forward * z;
  69.         PlayerController.Move(move * speed * Time.deltaTime); // ПЕРЕМЕЩЕНИЕ
  70.  
  71.         x = Input.GetAxis("Horizontal");
  72.         z = Input.GetAxis("Vertical");
  73.     }
  74.     //void Jumping()
  75.     //{
  76.     //    if (Input.GetKey(KeyCode.Space))
  77.     //    {
  78.     //        jumpForce = Jump;
  79.     //        //Debug.Log("Jumping");
  80.     //    }
  81.     //    else if (isGrounded) jumpForce = 0;
  82.     //    PlayerController.Move(Vector3.up * jumpForce * Time.deltaTime);
  83.     //}
  84.     void SmoothFall()
  85.     {
  86.         if (isGrounded && velocity.y < 0) velocity.y = -2f;
  87.         velocity.y += gravity * Time.deltaTime;
  88.         PlayerController.Move(velocity * Time.deltaTime);
  89.     }
  90.     void Crouch()
  91.     {
  92.  
  93.  
  94.         if (Input.GetKey(KeyCode.LeftControl))
  95.         {
  96.             this.transform.localScale = new Vector3(1, crouchSize, 1);
  97.         }
  98.         else this.transform.localScale = new Vector3(1, 1, 1);
  99.     }
  100.     void Teleportation()
  101.     {
  102.         if (Input.GetKey(KeyCode.R)) this.transform.position = spawnPointInfo;
  103.         if (inTrigger)
  104.         {
  105.             Debug.Log("U in the Trigger zone");
  106.             this.transform.position = spawnPoint.transform.position;
  107.         }
  108.     }
  109.     void Noclip()
  110.     {
  111.  
  112.     }
  113.     void Sprint()
  114.     {
  115.  
  116.  
  117.  
  118.     }
  119.  
  120.     void OnControllerColliderHit(ControllerColliderHit hit)
  121.     {
  122.         Rigidbody body = hit.collider.attachedRigidbody;
  123.  
  124.         // no rigidbody
  125.         if (body == null || body.isKinematic)
  126.             return;
  127.  
  128.         // We dont want to push objects below us
  129.         if (hit.moveDirection.y < -0.3f)
  130.             return;
  131.  
  132.         // Calculate push direction from move direction,
  133.         // we only push objects to the sides never up and down
  134.         Vector3 pushDir = new Vector3(hit.moveDirection.x, 0, hit.moveDirection.z);
  135.  
  136.         // If you know how fast your character is trying to move,
  137.         // then you can also multiply the push velocity by that.
  138.  
  139.         // Apply the push
  140.         body.velocity = pushDir * pushPower;
  141.     } // Взаимодействие с другими обьектами на карте
  142.     void AlternativeCheckGround()
  143.     {
  144.         isGrounded = Physics.Raycast(this.transform.position, Vector3.down, rayDistance, groundMask);
  145.     }
  146.  
  147.     void Jumping()
  148.     {
  149.         if (Input.GetKey(KeyCode.Space))
  150.         {
  151.             jumpForce = Jump;
  152.             //Debug.Log("Jumping");
  153.         }
  154.         else if (isGrounded) jumpForce = 0;
  155.         PlayerController.Move(Vector3.up * jumpForce * Time.deltaTime);
  156.  
  157.     }
  158. }
  159. // ПРОБЛЕМА С RIGIDBODY РЕШЕНА ПОСРЕДСТВОМ ВВЕДЕНИЯ ПУСТЫШКИ С РИДЖИДБОДИ ПОВЕРХ ИГРОКА
  160. // ГРАВИТАЦИЯ СДЕЛАНА
  161. // ПРЫЖКИ СДЕЛАНЫ
  162.     // НЕОБХОДИМО СДЕЛАТЬ ОГРАНИЧЕНИЕ ИЗМЕНЕНИЯ СКОРОСТИ ПРИ ПОЛЕТЕ (ЧТОБЫ ОНА МЕДЛЕННЕЕ ИЗМЕНЯЛАСЬ В ПОЛЕТЕ)
  163.     // НЕОБХОДИМО СДЕЛАТЬ ТЕЛЕПОРТАЦИЮ НА СПАВН (ПРОБЛЕМА - ПРИ СОБЛЮДЕНИИ УСЛОВИЯ НА ТЕЛЕПОРТАЦИИ - ВМЕСТО
  164.     // САМОЙ ТЕЛЕПОРТАЦИИ ПРОСТО ЗАМЕДЛЯЕТСЯ СКОРОСТЬ)
  165.  
  166.     // ПОЯВИЛАСЬ НОВАЯ ПРОБЛЕМА - ПРИ БЕГЕ + ПРЫЖКЕ И СОПРИКОСНОВЕНИИ С СТАТИЧНЫМ ОБЬЕКТОМ НЕКОРРЕКТНО РАБОТАЕТ ПРЫЖОК
  167.     // (ВОЗМОЖНОЕ РЕШЕНИЕ - УМЕНЬШЕНИЕ ДИАМЕТРА GROUNDCHECK ПОД ИГРОКОМ)
  168.  
  169.  
  170.  
  171.  
  172.     // КОД ОГРАНИЧЕНИЯ УПРАВЛЕНИЯ ПРИ ПОЛЕТЕ
  173.  
  174.     //if (movingOn)
  175.     //    {
  176.     //        x = Input.GetAxis("Horizontal");
  177.     //        z = Input.GetAxis("Vertical");
  178.     //    }
  179.  
  180.     //    if (!isGrounded)
  181.     //    {
  182.     //        reservX = x / 2;
  183.     //        reservZ = z / 2;
  184.  
  185.     //        movingOn = false;
  186.     //        move = transform.right* reservX + transform.forward* reservZ;
  187.     //    }
  188.     //    if (isGrounded & !movingOn)
  189.     //    {
  190.     //        movingOn = true;
  191.     //        x = reservX;
  192.     //        z = reservZ;
  193.     //    }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement