Advertisement
kadyr

Untitled

Oct 12th, 2021
813
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.95 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. using UnityEngine.SceneManagement;
  5.  
  6. public class Move : MonoBehaviour
  7. {
  8.     private Rigidbody rb; // ��������� Rigidbody
  9.     public float speed = 15;
  10.     Vector3 direction;
  11.     [SerializeField] Vector3 startPosition;
  12.     float timer;
  13.     [SerializeField] GameObject player;
  14.     float speedRotation;
  15.     float jumpSpeed;
  16.     [SerializeField] Animator Player;
  17.     float turnSmoothTime = 0.1f;
  18.     float turnSmoothVelocity;
  19.     [SerializeField] private Camera camera;
  20.     [SerializeField] private float step;
  21.  
  22.  
  23.     void Start()
  24.     {
  25.         startPosition = transform.position;
  26.         rb = GetComponent<Rigidbody>(); //������� ��������� � ������� � ����������
  27.     }
  28.  
  29.     Vector3 MousePosInWorldSpace()
  30.     {
  31.  
  32.         return Camera.main.ScreenToWorldPoint(Input.mousePosition);
  33.  
  34.     }
  35.  
  36.     private void Update()
  37.     {
  38.  
  39.         Vector3 mouse = Input.mousePosition;
  40.         Ray castPoint = Camera.main.ScreenPointToRay(mouse);
  41.         RaycastHit hit;
  42.         if (Physics.Raycast(castPoint, out hit, Mathf.Infinity))
  43.         {
  44.             CalucateDeltaAngle(hit.point);
  45.         }
  46.  
  47.         //��������� ��� �� �� ����� (���������� �����)
  48.         if (Physics.Raycast(transform.position, -Vector3.up, 1 + 0.1f))
  49.         {
  50.             //������� �� ������
  51.             if (Input.GetKeyDown(KeyCode.Space))
  52.             {
  53.                 rb.AddForce(new Vector3(0, 8, 0), ForceMode.Impulse);
  54.             }
  55.  
  56.         }
  57.         //���� �������� ���� �� �������� ��� ������� �� �����
  58.         if (transform.position.y < -10)
  59.         {
  60.             transform.position = new Vector3(5,5,5);
  61.         }
  62.  
  63.         if (Input.GetKeyDown(KeyCode.LeftShift))
  64.         {
  65.             speed = 30;
  66.         }
  67.         if (Input.GetKeyUp(KeyCode.LeftShift))
  68.         {
  69.             speed = 15;
  70.         }
  71.         float horizontal = Input.GetAxis("Horizontal");
  72.         float vertical = Input.GetAxis("Vertical");
  73.  
  74.        
  75.  
  76.         direction = transform.TransformDirection(horizontal, 0, vertical);
  77.         rb.MovePosition(transform.position + speed * direction * Time.deltaTime);
  78.  
  79.         timer += Time.deltaTime;
  80.     }
  81.     private void OnTriggerEnter(Collider collider)
  82.     {
  83.        
  84.     }
  85.  
  86.     private void RotateTowards(float angle)
  87.     {
  88.         Vector3 euler = transform.eulerAngles;
  89.         euler.y = Mathf.Round(angle / step) * step;
  90.         transform.eulerAngles = euler;
  91.     }
  92.  
  93.     private void CalucateDeltaAngle(Vector3 inputPosition)
  94.     {
  95.         Vector3 delta = transform.position - inputPosition;
  96.         RotateTowards(Quaternion.LookRotation(delta, transform.up).eulerAngles.y);
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement