diff --git a/3D Beginner/Assets/Prefabs/JohnLemon.prefab b/3D Beginner/Assets/Prefabs/JohnLemon.prefab index 3dbfe86d175e7fbc7297b42156b279b2d646cdf1..8e004f2e5626d95d9468992c5f5877fa0470a20d 100644 --- a/3D Beginner/Assets/Prefabs/JohnLemon.prefab +++ b/3D Beginner/Assets/Prefabs/JohnLemon.prefab @@ -1151,6 +1151,8 @@ GameObject: - component: {fileID: 5706511175008429661} - component: {fileID: 5706511175017712053} - component: {fileID: 4878180692424765429} + - component: {fileID: 160319675464144545} + - component: {fileID: 5735409784826791332} m_Layer: 0 m_Name: JohnLemon m_TagString: Untagged @@ -1207,8 +1209,35 @@ Rigidbody: m_UseGravity: 1 m_IsKinematic: 0 m_Interpolate: 0 - m_Constraints: 0 + m_Constraints: 84 m_CollisionDetection: 0 +--- !u!136 &160319675464144545 +CapsuleCollider: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 5706511175008336509} + m_Material: {fileID: 0} + m_IsTrigger: 0 + m_Enabled: 1 + m_Radius: 0.4 + m_Height: 1.4 + m_Direction: 1 + m_Center: {x: 0, y: 0.7, z: 0} +--- !u!114 &5735409784826791332 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 5706511175008336509} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 3afae084f9a58f74b870ea0d6d6026f6, type: 3} + m_Name: + m_EditorClassIdentifier: + turnSpeed: 20 --- !u!1 &5706511175008336511 GameObject: m_ObjectHideFlags: 0 diff --git a/3D Beginner/Assets/Scripts/PlayerMovement.cs b/3D Beginner/Assets/Scripts/PlayerMovement.cs new file mode 100644 index 0000000000000000000000000000000000000000..7bac9e1ce96c2eeb4f470bb60616f30bf15d12c2 --- /dev/null +++ b/3D Beginner/Assets/Scripts/PlayerMovement.cs @@ -0,0 +1,47 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class PlayerMovement : MonoBehaviour +{ + // non-public member variable --> starts with m_ prefix + Vector3 m_Movement; + Quaternion m_Rotation = Quaternion.identity; // Quaternions are a way of storing rotations; Quaternion.identity is simply giving it a value of no rotation + Animator m_Animator; + Rigidbody m_Rigidbody; + + public float turnSpeed = 20f; // the angle in radians you want the character to turn per second + + // Start is called before the first frame update + void Start() + { + m_Animator = GetComponent<Animator>(); + m_Rigidbody = GetComponent<Rigidbody>(); + } + + // FixedUpdate is called before the physics system solves any collisions and other interactions that have happened. By default it is called exactly 50 times every second. + void FixedUpdate() + { + float horizontal = Input.GetAxis("Horizontal"); // input axes return a number between -1 and 1 + float vertical = Input.GetAxis("Vertical"); + + m_Movement.Set(horizontal, 0f, vertical); // movement vector now has a value of the horizontal input in the x axis, 0 in the y axis and the vertical input in the z axis + m_Movement.Normalize(); // Normalizing a vector means keeping the vector’s direction the same, but changing its magnitude to 1 + + bool hasHorizontalInput = !Mathf.Approximately(horizontal, 0f); // determines whether or not there is horizontal input; !Approximately method will return true if the horizontal variable is approximately non-zero + bool hasVerticalInput = !Mathf.Approximately(vertical, 0f); + bool isWalking = hasHorizontalInput || hasVerticalInput; // if hasHorizontalInput or hasVerticalInput are true then isWalking is true + m_Animator.SetBool("IsWalking", isWalking); // The first parameter is the name of the Animator Parameter that you want to set the value of, and the second is the value you want to set it to + + Vector3 desiredForward = Vector3.RotateTowards(transform.forward, m_Movement, turnSpeed * Time.deltaTime, 0f); + m_Rotation = Quaternion.LookRotation(desiredForward); + } + + // OnAnimatorMove allows you to apply root motion as you want, which means that movement and rotation can be applied separately + void OnAnimatorMove() + { + m_Rigidbody.MovePosition(m_Rigidbody.position + m_Movement * m_Animator.deltaPosition.magnitude); + m_Rigidbody.MoveRotation(m_Rotation); + } + +} diff --git a/3D Beginner/Assets/Scripts/PlayerMovement.cs.meta b/3D Beginner/Assets/Scripts/PlayerMovement.cs.meta new file mode 100644 index 0000000000000000000000000000000000000000..1256588ebd598adc71a566dfc69bb812e36d0c1f --- /dev/null +++ b/3D Beginner/Assets/Scripts/PlayerMovement.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 3afae084f9a58f74b870ea0d6d6026f6 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: