123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- using UnityEngine;
- namespace TriLibCore.Samples
- {
-
- public class AvatarController : MonoBehaviour
- {
-
- public static AvatarController Instance { get; private set; }
-
-
-
- private const float MaxSpeed = 2f;
-
-
-
- private const float Acceleration = 5f;
-
-
-
- private const float Friction = 2f;
-
-
-
- private const float RotationSpeed = 60f;
-
-
-
- public CharacterController CharacterController;
-
-
-
- public Animator Animator;
-
-
-
- public GameObject InnerAvatar;
-
-
-
- private Vector3 _cameraOffset;
-
-
-
- private float _speed;
-
-
-
- private Vector3 _cameraHeightOffset;
-
-
-
- private float _currentVelocity;
-
- private void Awake()
- {
- Instance = this;
- _cameraHeightOffset = new Vector3(0f, CharacterController.height * 0.9f, 0f);
- _cameraOffset = Camera.main.transform.position - transform.position;
- }
-
- private void Update()
- {
- var input = new Vector3(Input.GetAxisRaw("Horizontal"), 0f, Input.GetAxisRaw("Vertical"));
- var direction = Camera.main.transform.TransformDirection(input);
- direction.y = 0f;
- direction.Normalize();
- var targetEulerAngles = direction.magnitude > 0 ? Quaternion.LookRotation(direction).eulerAngles : transform.rotation.eulerAngles;
- var eulerAngles = transform.rotation.eulerAngles;
- eulerAngles.y = Mathf.SmoothDampAngle(eulerAngles.y, targetEulerAngles.y, ref _currentVelocity, Time.deltaTime * RotationSpeed * input.magnitude);
- transform.rotation = Quaternion.Euler(eulerAngles);
- _speed += input.magnitude * (Acceleration * MaxSpeed) * Time.deltaTime;
- _speed -= Friction * MaxSpeed * Time.deltaTime;
- _speed = Mathf.Clamp(_speed, 0f, MaxSpeed);
- CharacterController.SimpleMove(transform.forward * _speed);
- Animator.SetFloat("SpeedFactor", _speed / MaxSpeed);
- var pivotedPosition = Quaternion.AngleAxis(AssetViewerBase.Instance.CameraAngle.x, Vector3.up) * Quaternion.AngleAxis(-AssetViewerBase.Instance.CameraAngle.y, Vector3.right) * _cameraOffset;
- Camera.main.transform.position = transform.position + _cameraHeightOffset + pivotedPosition;
- Camera.main.transform.LookAt(transform.position + _cameraHeightOffset);
- }
- }
- }
|