using UnityEngine;
public class Mover : MonoBehaviour
{
Rigidbody _rb;
float _currentSpeed;
[SerializeField] float _walkSpeed=5f;
[SerializeField] float _runSpeed=7f;
[SerializeField] float _sprintSpeed=10f;
Vector2 _inputVec2;
Vector3 _moveVecto3;
bool _sprint;
bool _superStrint;
bool _lastUpdateSuperPos;
float _currentSuperSprintTime;
float _superSprimtTime = 1f;
Animator _anim;
private void Start()
{
_rb = GetComponent<Rigidbody>();
_anim = GetComponent<Animator>();
}
private void Update()
{
InputRead();
SpeedControll();
AnimUpdate();
}
void InputRead()
{
_sprint=Input.GetKey(KeyCode.LeftShift);
_superStrint =Input.GetKey(KeyCode.LeftAlt);
if (_superStrint && !_lastUpdateSuperPos) _currentSuperSprintTime = _superSprimtTime;
_inputVec2.Set(Input.GetAxis("Horizontal"),Input.GetAxis("Vertical"));
_lastUpdateSuperPos = _superStrint;
}
void AnimUpdate()
{
_anim.SetFloat("MoveFront",_inputVec2.x);
_anim.SetFloat("MoveSIde", _inputVec2.y);
if (IsMove())
{
if (_inputVec2.sqrMagnitude > 1f) _inputVec2.Normalize();
}
_anim.SetFloat("MovePower", _currentSpeed);
}
void SpeedControll()
{
_currentSpeed = _sprint ? _sprintSpeed : _walkSpeed;
if (_superStrint && !(_currentSuperSprintTime > 0))
{
_currentSpeed = _sprintSpeed;
_currentSuperSprintTime -= Time.deltaTime;
}
}
private void FixedUpdate()
{
_moveVecto3.Set(_inputVec2.x, 0, _inputVec2.y);
_rb.MovePosition(_rb.position+_moveVecto3 * Time.fixedDeltaTime * _currentSpeed);
}
bool IsMove()
{
return _inputVec2.sqrMagnitude != 0;
}
}
тогда сохранится этот объект и все его дочерние, при загрузке новой сцены