Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PlayerControl_CharacterController : MonoBehaviour //Реализация перемещения с помощью CharacterController
- {
- public CharacterController PlayerController;
- public float speed = 7; //Рекомендуемое значение 7f
- public float gravity = -9.8f; //Рекомендуемое значение -9.8f
- public float Jump = 6.5f; //Рекомендуемое значение 6.5f
- float jumpForce;
- public float crouchSize = 0.8f; //Рекомендуемое значение 0.8f
- public float x;
- public float z;
- public float reservX;
- public float reservZ;
- public Transform groundCheck;
- public float groundDistance = 0.4f;
- public LayerMask triggerMask;
- public LayerMask groundMask;
- public bool isGrounded;
- public bool inTrigger;
- public bool movingOn;
- public bool shiftOn;
- public float rayDistance; //Рекомендуемое значение 1.2f
- public float pushPower = 5;
- public bool noclipOn = false;
- public GameObject spawnPoint;
- public GameObject PlayerCamera;
- public Vector3 spawnPointInfo;
- Vector3 velocity;
- void Start()
- {
- }
- public void Update()
- {
- //isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
- //inTrigger = Physics.CheckSphere(groundCheck.position, groundDistance, triggerMask);
- Cursor.visible = false;
- Moving();
- SmoothFall();
- Teleportation();
- Crouch();
- Noclip();
- AlternativeCheckGround();
- Jumping();
- }
- void inAirSpeed()
- {
- //Перемещение во время полета
- }
- void Moving()
- {
- Vector3 move = transform.right * x + transform.forward * z;
- PlayerController.Move(move * speed * Time.deltaTime); // ПЕРЕМЕЩЕНИЕ
- x = Input.GetAxis("Horizontal");
- z = Input.GetAxis("Vertical");
- }
- //void Jumping()
- //{
- // if (Input.GetKey(KeyCode.Space))
- // {
- // jumpForce = Jump;
- // //Debug.Log("Jumping");
- // }
- // else if (isGrounded) jumpForce = 0;
- // PlayerController.Move(Vector3.up * jumpForce * Time.deltaTime);
- //}
- void SmoothFall()
- {
- if (isGrounded && velocity.y < 0) velocity.y = -2f;
- velocity.y += gravity * Time.deltaTime;
- PlayerController.Move(velocity * Time.deltaTime);
- }
- void Crouch()
- {
- if (Input.GetKey(KeyCode.LeftControl))
- {
- this.transform.localScale = new Vector3(1, crouchSize, 1);
- }
- else this.transform.localScale = new Vector3(1, 1, 1);
- }
- void Teleportation()
- {
- if (Input.GetKey(KeyCode.R)) this.transform.position = spawnPointInfo;
- if (inTrigger)
- {
- Debug.Log("U in the Trigger zone");
- this.transform.position = spawnPoint.transform.position;
- }
- }
- void Noclip()
- {
- }
- void Sprint()
- {
- }
- void OnControllerColliderHit(ControllerColliderHit hit)
- {
- Rigidbody body = hit.collider.attachedRigidbody;
- // no rigidbody
- if (body == null || body.isKinematic)
- return;
- // We dont want to push objects below us
- if (hit.moveDirection.y < -0.3f)
- return;
- // Calculate push direction from move direction,
- // we only push objects to the sides never up and down
- Vector3 pushDir = new Vector3(hit.moveDirection.x, 0, hit.moveDirection.z);
- // If you know how fast your character is trying to move,
- // then you can also multiply the push velocity by that.
- // Apply the push
- body.velocity = pushDir * pushPower;
- } // Взаимодействие с другими обьектами на карте
- void AlternativeCheckGround()
- {
- isGrounded = Physics.Raycast(this.transform.position, Vector3.down, rayDistance, groundMask);
- }
- void Jumping()
- {
- if (Input.GetKey(KeyCode.Space))
- {
- jumpForce = Jump;
- //Debug.Log("Jumping");
- }
- else if (isGrounded) jumpForce = 0;
- PlayerController.Move(Vector3.up * jumpForce * Time.deltaTime);
- }
- }
- // ПРОБЛЕМА С RIGIDBODY РЕШЕНА ПОСРЕДСТВОМ ВВЕДЕНИЯ ПУСТЫШКИ С РИДЖИДБОДИ ПОВЕРХ ИГРОКА
- // ГРАВИТАЦИЯ СДЕЛАНА
- // ПРЫЖКИ СДЕЛАНЫ
- // НЕОБХОДИМО СДЕЛАТЬ ОГРАНИЧЕНИЕ ИЗМЕНЕНИЯ СКОРОСТИ ПРИ ПОЛЕТЕ (ЧТОБЫ ОНА МЕДЛЕННЕЕ ИЗМЕНЯЛАСЬ В ПОЛЕТЕ)
- // НЕОБХОДИМО СДЕЛАТЬ ТЕЛЕПОРТАЦИЮ НА СПАВН (ПРОБЛЕМА - ПРИ СОБЛЮДЕНИИ УСЛОВИЯ НА ТЕЛЕПОРТАЦИИ - ВМЕСТО
- // САМОЙ ТЕЛЕПОРТАЦИИ ПРОСТО ЗАМЕДЛЯЕТСЯ СКОРОСТЬ)
- // ПОЯВИЛАСЬ НОВАЯ ПРОБЛЕМА - ПРИ БЕГЕ + ПРЫЖКЕ И СОПРИКОСНОВЕНИИ С СТАТИЧНЫМ ОБЬЕКТОМ НЕКОРРЕКТНО РАБОТАЕТ ПРЫЖОК
- // (ВОЗМОЖНОЕ РЕШЕНИЕ - УМЕНЬШЕНИЕ ДИАМЕТРА GROUNDCHECK ПОД ИГРОКОМ)
- // КОД ОГРАНИЧЕНИЯ УПРАВЛЕНИЯ ПРИ ПОЛЕТЕ
- //if (movingOn)
- // {
- // x = Input.GetAxis("Horizontal");
- // z = Input.GetAxis("Vertical");
- // }
- // if (!isGrounded)
- // {
- // reservX = x / 2;
- // reservZ = z / 2;
- // movingOn = false;
- // move = transform.right* reservX + transform.forward* reservZ;
- // }
- // if (isGrounded & !movingOn)
- // {
- // movingOn = true;
- // x = reservX;
- // z = reservZ;
- // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement