Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class PlayerController : MonoBehaviour {
- public float speed = 6f;
- protected Rigidbody playerRB;
- protected Vector3 movement;
- protected Vector3 direction;
- protected void Start () {
- playerRB = GetComponent<Rigidbody>();
- }
- // Update is called once per frame
- void FixedUpdate () {
- Move();
- }
- protected void Move()
- {
- float x = Input.GetAxisRaw("Horizontal");
- float z = Input.GetAxisRaw("Vertical");
- Vector3 newPos = new Vector3(x, 0f, z);
- newPos = Camera.main.transform.TransformDirection(newPos);
- movement.Set(newPos.x, newPos.y, newPos.z);
- movement = movement.normalized * Time.deltaTime;
- direction = new Vector3(x, 0f, z);
- direction = Camera.main.transform.TransformDirection(direction);
- direction = direction.normalized;
- if (direction.sqrMagnitude > 0.01){
- playerRB.transform.rotation = Quaternion.Slerp(playerRB.transform.rotation, Quaternion.LookRotation(direction), Time.deltaTime);
- }
- playerRB.MovePosition(movement + transform.position);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement