Advertisement
AL4ST4I2

PlayerController

Oct 29th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.07 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class PlayerController : MonoBehaviour {
  5.  
  6.     public float speed = 6f;
  7.     protected Rigidbody playerRB;
  8.  
  9.     protected Vector3 movement;
  10.     protected Vector3 direction;
  11.  
  12.  
  13.     protected void Start () {
  14.         playerRB = GetComponent<Rigidbody>();
  15.     }
  16.    
  17.     // Update is called once per frame
  18.     void FixedUpdate () {
  19.         Move();
  20.     }
  21.  
  22.     protected void Move()
  23.     {
  24.        
  25.        
  26.         float x = Input.GetAxisRaw("Horizontal");
  27.         float z = Input.GetAxisRaw("Vertical");
  28.         Vector3 newPos = new Vector3(x, 0f, z);
  29.         newPos = Camera.main.transform.TransformDirection(newPos);
  30.         movement.Set(newPos.x, newPos.y, newPos.z);
  31.         movement = movement.normalized * Time.deltaTime;
  32.  
  33.         direction = new Vector3(x, 0f, z);
  34.         direction = Camera.main.transform.TransformDirection(direction);
  35.         direction = direction.normalized;
  36.  
  37.         if (direction.sqrMagnitude > 0.01){
  38.             playerRB.transform.rotation = Quaternion.Slerp(playerRB.transform.rotation, Quaternion.LookRotation(direction), Time.deltaTime);
  39.         }
  40.  
  41.         playerRB.MovePosition(movement + transform.position);
  42.  
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement