Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class tp_motor : MonoBehaviour {
- public static tp_motor Instance;
- public float MoveSpeed = 10f;
- public float turnSpeed = 5.0f;
- public Transform target;
- private Vector3 relativePos = new Vector3(0,0,0);
- private Vector3 MoveIncrement = new Vector3(0,0,0);
- private Vector3 lastDirection = new Vector3(0,0,0);
- public Vector3 MoveVector {get; set;}
- // Use this for initialization
- void Awake () {
- Instance = this;
- }
- // Update is called once per frame
- public void UpdateMotor() {
- animationhandle();
- snapalign();
- ProcessMotion();
- rotatingPlayer();
- }
- void ProcessMotion()
- {
- //transform move vector into world space
- MoveVector = transform.TransformDirection(MoveVector);
- //I need to make rotation do the same thing the moving did here!
- if(MoveVector.magnitude >1)
- MoveVector = Vector3.Normalize(MoveVector);
- MoveVector *= MoveSpeed;
- MoveVector *= Time.deltaTime;
- // this is what ultimately moves the object VVV
- tp_controller.CharacterController.Move(MoveVector);
- }
- void rotatingPlayer(){
- var x = Input.GetAxis("Horizontal");
- var z = Input.GetAxis("Vertical");
- //get the last rotation input from WASD (it's using axis so I don't know if I can)
- if(z == 0 && x == 0)
- transform.LookAt(lastDirection);
- else
- {
- //I need to get the half value of the rotation
- // moveVector / 2
- // Then I need to make the transform.lookAt increment
- //if (transform.position != MoveVector then add increment)
- // transform.position += increment =
- // ^^ this wouldn't work
- MoveIncrement = MoveVector ;
- transform.LookAt(transform.position + MoveIncrement);
- //transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(transform.position + MoveVector), 0);
- //transform.rotation = Vector3.Slerp(transform.position, MoveVector, 5.0f *Time.deltaTime);
- // ^^these didn't work
- lastDirection = (transform.position + MoveIncrement);
- // ^^ this value needs to stop updating as soon as the if statement returns true because it's snapping back to 0
- // or rather lastDirect needs to be some sort of seperate value that only updates once.
- }
- //Loop, get rotation. set rotation, update rotation.
- }
- void animationhandle(){
- if(MoveVector.x != 0 || MoveVector.z !=0)
- animation.CrossFade("Run", 0.2f);
- if(MoveVector.x == 0 && MoveVector.z == 0)
- animation.CrossFade("Idiling", 0.2f);
- }
- void snapalign()
- {
- 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