Guest User

Untitled

a guest
May 28th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.12 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class TP_Motor : MonoBehaviour
  5. {
  6.     public static TP_Motor Instance;
  7.    
  8.     public float MoveSpeed = 10f;
  9.    
  10.     public Vector3 MoveVector { get; set; }
  11.  
  12.     void Awake()
  13.     {
  14.         Instance = this;
  15.     }
  16.    
  17.     void UpdateMotor()
  18.     {
  19.         ProcessMotion();   
  20.         SnapAlignCharacterWithCamera();
  21.     }
  22.    
  23.     void ProcessMotion()
  24.     {
  25.         // transform MoveVector to world space
  26.         MoveVector = transform.TransformDirection(MoveVector);
  27.        
  28.         // Normalize movevector if magnitude is greater than 1
  29.         if (MoveSpeed.magnitude > 1)
  30.             MoveVector = Vector3.Normalize(MoveVector);
  31.        
  32.         // Multiply MoveVector by MoveSpeed
  33.         MoveVector *= MoveSpeed;
  34.        
  35.         // multiply MoveVector by Deltatime
  36.         MoveVector *= Time.deltaTime;
  37.        
  38.         // Move the Character in World Space
  39.         TP_Controller.characterController.Move(MoveVector);
  40.     }
  41.    
  42.     void SnapAlignCharacterWithCamera()
  43.     {
  44.         if (MoveVector.x !=0 || MoveVector.z !=0)
  45.         {
  46.             transform.rotation = Quaternion.Euler(transform.eulerAngles.x,
  47.                                              camera.mainCamera.transform.eulerAngles.y,
  48.                                              transform.eulerAngles.z);
  49.         }
  50.     }
  51. }
Add Comment
Please, Sign In to add comment