From 7290f575af147bbf94509cd39cf026c1bfc252f6 Mon Sep 17 00:00:00 2001 From: Anna Matela <N2970@student.jamk.fi> Date: Tue, 26 Jan 2021 22:51:11 +0200 Subject: [PATCH] player character is finished --- 3D Beginner/Assets/Prefabs/JohnLemon.prefab | 31 +++++++++++- 3D Beginner/Assets/Scripts/PlayerMovement.cs | 47 +++++++++++++++++++ .../Assets/Scripts/PlayerMovement.cs.meta | 11 +++++ 3 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 3D Beginner/Assets/Scripts/PlayerMovement.cs create mode 100644 3D Beginner/Assets/Scripts/PlayerMovement.cs.meta diff --git a/3D Beginner/Assets/Prefabs/JohnLemon.prefab b/3D Beginner/Assets/Prefabs/JohnLemon.prefab index 3dbfe86..8e004f2 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 0000000..7bac9e1 --- /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 0000000..1256588 --- /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: -- GitLab