Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class TP_Controller : MonoBehaviour
- {
- public static CharacterController CharacterController;
- public static TP_Controller Instance;
- void Awake ()
- {
- CharacterController = GetComponent("CharacterController") as CharacterController;
- Instance = this;
- }
- void Update ()
- {
- if (Camera.main == null)
- return;
- GetLocomotionInput();
- TP_Motor.Instance.UpdateMotor();
- }
- void GetLocomotionInput()
- {
- var deadZone = 0.1f;
- TP_Motor.Instance.MoveVector = Vector3.zero;
- if (Input.GetAxis ("Vertical") > deadZone || Input.GetAxis("Vertical") < -deadZone)
- TP_Motor.Instance.MoveVector += new Vector3(0, 0, Input.GetAxis("Vertical"));
- if (Input.GetAxis ("Horizontal") > deadZone || Input.GetAxis("Horizontal") < -deadZone)
- TP_Motor.Instance.MoveVector += new Vector3(Input.GetAxis("Horizontal"), 0, 0);
- }
- }
- ------------------------------------
- using UnityEngine;
- using System.Collections;
- public class TP_Motor : MonoBehaviour
- {
- public static TP_Motor Instance;
- public float MoveSpeed = 10f;
- public Vector3 MoveVector { get; set; }
- void Awake ()
- {
- Instance = this;
- }
- public void UpdateMotor ()
- {
- SnapAlignCharacterWithCamera();
- ProcessMotion();
- }
- void ProcessMotion()
- {
- //transform MoveVector to World Space
- MoveVector = transform.TransformDirection(MoveVector);
- //normalize MoveVector if Magnitude > 1
- if(MoveVector.magnitude > 1)
- MoveVector = Vector3.Normalize(MoveVector);
- //Multiply MoveVector by MoveSpeed
- MoveVector *= MoveSpeed;
- //Multiply MoveVector by DeltaTime
- MoveVector *= Time.deltaTime;
- //Move the Character in World Space
- TP_Controller.CharacterController.Move(MoveVector);
- }
- void SnapAlignCharacterWithCamera()
- {
- if(MoveVector.x != 0 || MoveVector.z != 0)
- {
- transform.rotation = Quaternion.Euler(transform.eulerAngles.x,
- Camera.main.transform.eulerAngles.y,
- transform.eulerAngles.z);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement