Last active
August 29, 2015 14:26
-
-
Save seamanmur/a33891b4e596e341190a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CharMotor : MonoBehaviour | |
{ | |
public static CharMotor instance; | |
public float speed = 10.0f; | |
void Awake() | |
{ | |
instance = this; | |
} | |
public void UpdateMotor(Vector3 move) | |
{ | |
_RotateChar(move); | |
_ProcessMotion(move); | |
} | |
private void _ProcessMotion(Vector3 moveVector) | |
{ | |
//Преобразуем вектор движения в мировое пространство. | |
moveVector = transform.TransformDirection(moveVector); | |
//Нормализуем вектор движения | |
Vector3.Normalize(moveVector); | |
//Применяем скорость персонажа | |
moveVector *= speed; | |
//Переходим от кадров к секундам | |
moveVector *= Time.deltaTime; | |
//Двигаем! | |
CharController.unityController.Move(moveVector); | |
} | |
private void _RotateChar(Vector3 move) | |
{ | |
//Проверяем - двигается ли персонаж? | |
if(move.x != 0 || move.z != 0) | |
{ | |
//Если двигается - уставливаем его поворот в соответствии с | |
//поворотом камеры. Т.к. это нужно сделать только вокруг оси Y, | |
//а вокруг X и Z оставить неизменным, то используем метод, | |
//который конструирует вращение из трех углов | |
transform.rotation = Quaternion.Euler(transform.eulerAngles.x, | |
Camera.main.transform.eulerAngles.y, transform.eulerAngles.z); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment