Я писал один из скриптов отвечающий за передвижение и тут вот такое:
Вот код:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMotor : BaseMotor
{
protected override void UpdateMotor()
{
MoveVector = InputDirection();
Move();
}
private Vector3 InputDirection()
{
Vector3 dir = Vector.zero;
dir.x = Input.GetAxis("Horizontal");
dir.z = Input.GetAxis("Vertical");
if (dir.magnitude > 1)
dir.Normalize();
return dir;
}
Если надо то вот код BaseMotor:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public abstract class BaseMotor : MonoBehaviour
{
protected CharacterController controller;
protected Transform thisTransform;
private float baseSpeed = 7.0f;
private float baseGravity = 25.0f;
public float Speed { get { return baseSpeed; } }
public float Gravity { get { return baseGravity; } }
public Vector3 MoveVector{set; get;}
protected abstract void UpdateMotor();
protected virtual void Start()
{
controller = gameObject.AddComponent<CharacterController>();
thisTransform = transform;
}
private void Update()
{
UpdateMotor();
}
protected virtual void Move()
{
controller.Move(MoveVector * Time.deltaTime);
}
}