From b696d526d5d99746fd7874c5d094fd8a89369080 Mon Sep 17 00:00:00 2001 From: Gregory Campbell Date: Sun, 10 Nov 2019 21:04:52 -0500 Subject: [PATCH] Player moves based on where the camera is pointing and faces the direction in which it is moving --- Assets/Scenes/Test Scene.unity | 8 +++++--- Assets/Scripts/PlayerController.cs | 24 +++++++++++++++--------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/Assets/Scenes/Test Scene.unity b/Assets/Scenes/Test Scene.unity index 7b2fe55..f97dbff 100644 --- a/Assets/Scenes/Test Scene.unity +++ b/Assets/Scenes/Test Scene.unity @@ -147,8 +147,8 @@ Transform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 150911451} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 0, y: 0.5, z: 0.5} - m_LocalScale: {x: 0.5, y: 0.5, z: 0.5} + m_LocalPosition: {x: 0, y: 0.5, z: 0} + m_LocalScale: {x: 0.25, y: 0.5, z: 1.5} m_Children: [] m_Father: {fileID: 741217456} m_RootOrder: 0 @@ -538,7 +538,9 @@ MonoBehaviour: speed: 20 jumpSpeed: 20 gravity: 5 - rotationSpeed: 200 + rotationSpeed: 20 + pivot: {fileID: 1402695046} + playerModel: {fileID: 150911451} --- !u!143 &741217458 CharacterController: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/PlayerController.cs b/Assets/Scripts/PlayerController.cs index e6cd211..327d259 100644 --- a/Assets/Scripts/PlayerController.cs +++ b/Assets/Scripts/PlayerController.cs @@ -11,10 +11,12 @@ public class PlayerController : MonoBehaviour { CharacterController characterController; - public float speed = 20.0f; - public float jumpSpeed = 15.0f; - public float gravity = 5.0f; - public float rotationSpeed = 150.0f; + public float speed; + public float jumpSpeed; + public float gravity; + public float rotationSpeed; + public Transform pivot; + public GameObject playerModel; private bool extraJump = true; private Vector3 moveDirection; @@ -30,11 +32,6 @@ public class PlayerController : MonoBehaviour float yStore = moveDirection.y; moveDirection = (transform.forward * Input.GetAxisRaw("Vertical")) + (transform.right * Input.GetAxisRaw("Horizontal")); - //Player rotation - //transform.Rotate(0, Input.GetAxis("Horizontal") * Time.deltaTime * rotationSpeed, 0); - Vector3 newPosition = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical")); - transform.LookAt(newPosition + transform.position); - if (extraJump == true) { moveDirection = moveDirection.normalized * speed; //Remove this line to make running diagonal the fastest standard run @@ -75,5 +72,14 @@ public class PlayerController : MonoBehaviour // Move the controller characterController.Move(moveDirection * Time.deltaTime); + + //Move the player in different directions based on camera look direction + if(Input.GetAxisRaw("Horizontal") != 0 || Input.GetAxisRaw("Vertical") != 0) + { + transform.rotation = Quaternion.Euler(0f, pivot.rotation.eulerAngles.y, 0f); + Quaternion newRotation = Quaternion.LookRotation(new Vector3(moveDirection.x, 0f, moveDirection.z)); + playerModel.transform.rotation = Quaternion.Slerp(playerModel.transform.rotation, newRotation, rotationSpeed * Time.deltaTime); + } + } }