Advertisement
valik140201

Untitled

Nov 12th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.97 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. [RequireComponent(typeof(CharacterController))]
  5.  
  6.  
  7. public class RetiveMovement : MonoBehaviour {
  8.  
  9.     [SerializeField]
  10.     private Transform target;
  11.  
  12.    
  13.     public float rotSpeed = 15.0f;
  14.     public float moveSpeed = 6.0f;
  15.  
  16.     private CharacterController _characterController;
  17.  
  18.     void Start()
  19.     {
  20.         _characterController = GetComponent<CharacterController>();
  21.            
  22.     }
  23.  
  24.     void Update () {
  25.         Vector3 movement = Vector3.zero; // Начинаем с нулевых кординат
  26.  
  27.         float horInput = Input.GetAxis("Horizontal");
  28.         float vertInput = Input.GetAxis("Vertical");
  29.  
  30.  
  31.         if (horInput != 0 || vertInput != 0) // ДВижение работает только при нажатии стрелок на клв.
  32.         {
  33.             movement.x = horInput * moveSpeed;
  34.             movement.z = vertInput * moveSpeed;
  35.             movement = Vector3.ClampMagnitude(movement, moveSpeed); // ограничеваем движение по диогонали той же скоростью что и движение по оси
  36.  
  37.             Quaternion tmp = target.rotation; // Сохраняем изначальное вращение, чтобы вернуться к ней после завершении работы объекта
  38.             target.eulerAngles = new Vector3(0, target.eulerAngles.z, 0);
  39.             movement = target.TransformDirection(movement); // Преабразуем локальные кординаты в мировые
  40.  
  41.             target.rotation = tmp;
  42.            
  43.  
  44.             Quaternion direction = Quaternion.LookRotation(movement); // LookRotation(movement) - Плавный поворот
  45.             transform.rotation = Quaternion.Lerp(transform.rotation, direction, rotSpeed * Time.deltaTime);
  46.         }
  47.  
  48.         movement *= Time.deltaTime;
  49.         _characterController.Move(movement);
  50.  
  51.        
  52.     }
  53.  
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement