Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- [RequireComponent(typeof(CharacterController))]
- public class RetiveMovement : MonoBehaviour {
- [SerializeField]
- private Transform target;
- public float rotSpeed = 15.0f;
- public float moveSpeed = 6.0f;
- private CharacterController _characterController;
- void Start()
- {
- _characterController = GetComponent<CharacterController>();
- }
- void Update () {
- Vector3 movement = Vector3.zero; // Начинаем с нулевых кординат
- float horInput = Input.GetAxis("Horizontal");
- float vertInput = Input.GetAxis("Vertical");
- if (horInput != 0 || vertInput != 0) // ДВижение работает только при нажатии стрелок на клв.
- {
- movement.x = horInput * moveSpeed;
- movement.z = vertInput * moveSpeed;
- movement = Vector3.ClampMagnitude(movement, moveSpeed); // ограничеваем движение по диогонали той же скоростью что и движение по оси
- Quaternion tmp = target.rotation; // Сохраняем изначальное вращение, чтобы вернуться к ней после завершении работы объекта
- target.eulerAngles = new Vector3(0, target.eulerAngles.z, 0);
- movement = target.TransformDirection(movement); // Преабразуем локальные кординаты в мировые
- target.rotation = tmp;
- Quaternion direction = Quaternion.LookRotation(movement); // LookRotation(movement) - Плавный поворот
- transform.rotation = Quaternion.Lerp(transform.rotation, direction, rotSpeed * Time.deltaTime);
- }
- movement *= Time.deltaTime;
- _characterController.Move(movement);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement