Guest User

tp_motor

a guest
Jul 12th, 2014
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.73 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class tp_motor : MonoBehaviour {
  5.  
  6.  
  7.     public static tp_motor Instance;
  8.  
  9.     public float MoveSpeed = 10f;
  10.     public float turnSpeed = 5.0f;
  11.     public Transform target;
  12.     private Vector3 relativePos = new Vector3(0,0,0);
  13.     private Vector3 MoveIncrement = new Vector3(0,0,0);
  14.     private Vector3 lastDirection = new Vector3(0,0,0);
  15.  
  16.  
  17.  
  18.     public Vector3 MoveVector {get; set;}
  19.  
  20.  
  21.     // Use this for initialization
  22.     void Awake () {
  23.    
  24.         Instance = this;
  25.    
  26.     }
  27.    
  28.     // Update is called once per frame
  29.     public void UpdateMotor() {
  30.  
  31.         animationhandle();
  32.         snapalign();
  33.  
  34.         ProcessMotion();
  35.         rotatingPlayer();
  36.  
  37.     }
  38.  
  39.     void ProcessMotion()
  40.     {
  41.         //transform move vector into world space
  42.         MoveVector = transform.TransformDirection(MoveVector);
  43.  
  44.         //I need to make rotation do the same thing the moving did here!
  45.         if(MoveVector.magnitude >1)
  46.         MoveVector = Vector3.Normalize(MoveVector);
  47.  
  48.         MoveVector *= MoveSpeed;
  49.  
  50.         MoveVector *= Time.deltaTime;
  51.         // this is what ultimately moves the object VVV
  52.         tp_controller.CharacterController.Move(MoveVector);
  53.    
  54.  
  55.     }
  56.  
  57.     void rotatingPlayer(){
  58.  
  59.    
  60.         var x = Input.GetAxis("Horizontal");
  61.         var z = Input.GetAxis("Vertical");
  62.         //get the last rotation input from WASD (it's using axis so I don't know if I can)
  63.    
  64.         if(z == 0 && x == 0)
  65.  
  66.             transform.LookAt(lastDirection);
  67.  
  68.         else
  69.  
  70.  
  71.         {
  72.         //I need to get the half value of the rotation
  73.         // moveVector / 2
  74.         // Then I need to make the transform.lookAt increment
  75.             //if (transform.position != MoveVector then add increment)
  76.             // transform.position += increment =
  77.             // ^^ this wouldn't work
  78.  
  79.             MoveIncrement = MoveVector ;
  80.        
  81.  
  82.             transform.LookAt(transform.position + MoveIncrement);
  83.             //transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(transform.position + MoveVector), 0);
  84.             //transform.rotation = Vector3.Slerp(transform.position, MoveVector, 5.0f *Time.deltaTime);
  85.             // ^^these didn't work
  86.             lastDirection = (transform.position + MoveIncrement);
  87.             // ^^ this value needs to stop updating as soon as the if statement returns true because it's snapping back to 0
  88.             // or rather lastDirect needs to be some sort of seperate value that only updates once.
  89.         }
  90.  
  91.    
  92.  
  93.         //Loop, get rotation. set rotation, update rotation.
  94.  
  95.     }
  96.  
  97.     void animationhandle(){
  98.  
  99.        
  100.        
  101.         if(MoveVector.x != 0 || MoveVector.z !=0)
  102.             animation.CrossFade("Run", 0.2f);
  103.        
  104.         if(MoveVector.x == 0 && MoveVector.z == 0)
  105.             animation.CrossFade("Idiling", 0.2f);
  106.     }
  107.  
  108.     void snapalign()
  109.     {
  110.         if(MoveVector.x != 0 || MoveVector.z !=0)
  111.         {
  112.             transform.rotation = Quaternion.Euler(transform.eulerAngles.x,Camera.main.transform.eulerAngles.y, transform.eulerAngles.z);
  113.         }
  114.  
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment