Ken Schaefer
5 years ago
28 changed files with 420 additions and 67 deletions
@ -1,56 +1,65 @@
|
||||
/****************************************************************************** |
||||
* Initial code based on Brackey's FPS video |
||||
* https://youtu.be/_QajrabyTJc |
||||
* |
||||
* Notes: |
||||
* Refactored public fields to [SerializeField]. |
||||
* |
||||
* Refactored player and environment attributes to use Float References. This |
||||
* will allow other objects to buff/debuff these settings. |
||||
*****************************************************************************/ |
||||
|
||||
/****************************************************************************** |
||||
* |
||||
*****************************************************************************/ |
||||
using System.Collections; |
||||
using System.Collections.Generic; |
||||
using Architecture.Variables; |
||||
using UnityEngine; |
||||
|
||||
public class PlayerMovement : MonoBehaviour |
||||
{ |
||||
// Implement movement |
||||
public CharacterController controller; |
||||
public float speed = 12f; |
||||
|
||||
// Implement gravity |
||||
public float gravity = -9.81f; |
||||
private Vector3 velocity; |
||||
|
||||
// Implement ground reset of gravity |
||||
public Transform groundCheck; |
||||
public float groundDistance = 0.4f; |
||||
public LayerMask groundMask; |
||||
private bool isGrounded; |
||||
|
||||
// Implement jumping |
||||
public float jumpHeight = 3f; |
||||
|
||||
void Update() |
||||
{ |
||||
// Check if the player is on the ground and reset gravity |
||||
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask); |
||||
Debug.Log(isGrounded); |
||||
if (isGrounded && velocity.y < 0) |
||||
{ |
||||
velocity.y = -2f; |
||||
} |
||||
|
||||
// Implement movement |
||||
float x = Input.GetAxis("Horizontal"); |
||||
float z = Input.GetAxis("Vertical"); |
||||
|
||||
Vector3 move = transform.right * x + transform.forward * z; |
||||
controller.Move(move * speed * Time.deltaTime); |
||||
|
||||
// Implement jumping |
||||
if(Input.GetButtonDown("Jump") && isGrounded) |
||||
{ |
||||
Debug.Log("Jumping"); |
||||
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity); |
||||
} |
||||
|
||||
// Implement gravity |
||||
velocity.y += gravity * Time.deltaTime; |
||||
controller.Move(velocity * Time.deltaTime); |
||||
} |
||||
[Header("Character")] |
||||
[SerializeField] private CharacterController controller; |
||||
|
||||
[Header("Attributes")] |
||||
[SerializeField] private FloatReference playerSpeed; |
||||
[SerializeField] private FloatReference jumpHeight; |
||||
|
||||
[Header("Environment")] |
||||
[SerializeField] private FloatReference gravity; |
||||
private Vector3 velocity; |
||||
|
||||
[Header("Ground")] |
||||
[SerializeField] private Transform groundCheck; |
||||
[SerializeField] private float groundDistance = 0.4f; |
||||
[SerializeField] private LayerMask groundMask; |
||||
private bool isGrounded; |
||||
|
||||
void Update() |
||||
{ |
||||
// Check if the player is on the ground and reset gravity |
||||
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask); |
||||
if (isGrounded && velocity.y < 0) |
||||
{ |
||||
velocity.y = -2f; |
||||
} |
||||
|
||||
// Implement movement |
||||
float x = Input.GetAxis("Horizontal"); |
||||
float z = Input.GetAxis("Vertical"); |
||||
|
||||
Vector3 move = transform.right * x + transform.forward * z; |
||||
controller.Move(move * playerSpeed.Value * Time.deltaTime); |
||||
|
||||
// Implement jumping |
||||
if(Input.GetButtonDown("Jump") && isGrounded) |
||||
{ |
||||
velocity.y = Mathf.Sqrt(jumpHeight.Value * -2f * gravity.Value); |
||||
} |
||||
|
||||
// Implement gravity |
||||
velocity.y += gravity.Value * Time.deltaTime; |
||||
controller.Move(velocity * Time.deltaTime); |
||||
} |
||||
} |
||||
|
@ -0,0 +1,16 @@
|
||||
%YAML 1.1 |
||||
%TAG !u! tag:unity3d.com,2011: |
||||
--- !u!114 &11400000 |
||||
MonoBehaviour: |
||||
m_ObjectHideFlags: 0 |
||||
m_CorrespondingSourceObject: {fileID: 0} |
||||
m_PrefabInstance: {fileID: 0} |
||||
m_PrefabAsset: {fileID: 0} |
||||
m_GameObject: {fileID: 0} |
||||
m_Enabled: 1 |
||||
m_EditorHideFlags: 0 |
||||
m_Script: {fileID: 11500000, guid: 7cb18c6bb3c4bcd40b8451100e11b136, type: 3} |
||||
m_Name: GravityEarth |
||||
m_EditorClassIdentifier: |
||||
DeveloperDescription: |
||||
Value: -9.8 |
@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2 |
||||
guid: bc418284fb9af5f46b601970ea5d2868 |
||||
NativeFormatImporter: |
||||
externalObjects: {} |
||||
mainObjectFileID: 0 |
||||
userData: |
||||
assetBundleName: |
||||
assetBundleVariant: |
@ -0,0 +1,16 @@
|
||||
%YAML 1.1 |
||||
%TAG !u! tag:unity3d.com,2011: |
||||
--- !u!114 &11400000 |
||||
MonoBehaviour: |
||||
m_ObjectHideFlags: 0 |
||||
m_CorrespondingSourceObject: {fileID: 0} |
||||
m_PrefabInstance: {fileID: 0} |
||||
m_PrefabAsset: {fileID: 0} |
||||
m_GameObject: {fileID: 0} |
||||
m_Enabled: 1 |
||||
m_EditorHideFlags: 0 |
||||
m_Script: {fileID: 11500000, guid: 7cb18c6bb3c4bcd40b8451100e11b136, type: 3} |
||||
m_Name: GravityEarthMoon |
||||
m_EditorClassIdentifier: |
||||
DeveloperDescription: |
||||
Value: -1.6 |
@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2 |
||||
guid: ba6e18959c9b91d49bbb2b9897c0dd09 |
||||
NativeFormatImporter: |
||||
externalObjects: {} |
||||
mainObjectFileID: 0 |
||||
userData: |
||||
assetBundleName: |
||||
assetBundleVariant: |
@ -0,0 +1,16 @@
|
||||
%YAML 1.1 |
||||
%TAG !u! tag:unity3d.com,2011: |
||||
--- !u!114 &11400000 |
||||
MonoBehaviour: |
||||
m_ObjectHideFlags: 0 |
||||
m_CorrespondingSourceObject: {fileID: 0} |
||||
m_PrefabInstance: {fileID: 0} |
||||
m_PrefabAsset: {fileID: 0} |
||||
m_GameObject: {fileID: 0} |
||||
m_Enabled: 1 |
||||
m_EditorHideFlags: 0 |
||||
m_Script: {fileID: 11500000, guid: 7cb18c6bb3c4bcd40b8451100e11b136, type: 3} |
||||
m_Name: GravityJupiter |
||||
m_EditorClassIdentifier: |
||||
DeveloperDescription: |
||||
Value: -24.9 |
@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2 |
||||
guid: 89639fdc61d4c234cb28f16bfde630f8 |
||||
NativeFormatImporter: |
||||
externalObjects: {} |
||||
mainObjectFileID: 0 |
||||
userData: |
||||
assetBundleName: |
||||
assetBundleVariant: |
@ -0,0 +1,16 @@
|
||||
%YAML 1.1 |
||||
%TAG !u! tag:unity3d.com,2011: |
||||
--- !u!114 &11400000 |
||||
MonoBehaviour: |
||||
m_ObjectHideFlags: 0 |
||||
m_CorrespondingSourceObject: {fileID: 0} |
||||
m_PrefabInstance: {fileID: 0} |
||||
m_PrefabAsset: {fileID: 0} |
||||
m_GameObject: {fileID: 0} |
||||
m_Enabled: 1 |
||||
m_EditorHideFlags: 0 |
||||
m_Script: {fileID: 11500000, guid: 7cb18c6bb3c4bcd40b8451100e11b136, type: 3} |
||||
m_Name: GravityMars |
||||
m_EditorClassIdentifier: |
||||
DeveloperDescription: |
||||
Value: -3.8 |
@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2 |
||||
guid: ee3bf6f48ab93234cae8dd115088d905 |
||||
NativeFormatImporter: |
||||
externalObjects: {} |
||||
mainObjectFileID: 0 |
||||
userData: |
||||
assetBundleName: |
||||
assetBundleVariant: |
@ -0,0 +1,16 @@
|
||||
%YAML 1.1 |
||||
%TAG !u! tag:unity3d.com,2011: |
||||
--- !u!114 &11400000 |
||||
MonoBehaviour: |
||||
m_ObjectHideFlags: 0 |
||||
m_CorrespondingSourceObject: {fileID: 0} |
||||
m_PrefabInstance: {fileID: 0} |
||||
m_PrefabAsset: {fileID: 0} |
||||
m_GameObject: {fileID: 0} |
||||
m_Enabled: 1 |
||||
m_EditorHideFlags: 0 |
||||
m_Script: {fileID: 11500000, guid: 7cb18c6bb3c4bcd40b8451100e11b136, type: 3} |
||||
m_Name: GravityMercury |
||||
m_EditorClassIdentifier: |
||||
DeveloperDescription: |
||||
Value: -3.7 |
@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2 |
||||
guid: c3ef7ce93616e5e4ca66c56056fe9bbd |
||||
NativeFormatImporter: |
||||
externalObjects: {} |
||||
mainObjectFileID: 0 |
||||
userData: |
||||
assetBundleName: |
||||
assetBundleVariant: |
@ -0,0 +1,16 @@
|
||||
%YAML 1.1 |
||||
%TAG !u! tag:unity3d.com,2011: |
||||
--- !u!114 &11400000 |
||||
MonoBehaviour: |
||||
m_ObjectHideFlags: 0 |
||||
m_CorrespondingSourceObject: {fileID: 0} |
||||
m_PrefabInstance: {fileID: 0} |
||||
m_PrefabAsset: {fileID: 0} |
||||
m_GameObject: {fileID: 0} |
||||
m_Enabled: 1 |
||||
m_EditorHideFlags: 0 |
||||
m_Script: {fileID: 11500000, guid: 7cb18c6bb3c4bcd40b8451100e11b136, type: 3} |
||||
m_Name: GravityNeptune |
||||
m_EditorClassIdentifier: |
||||
DeveloperDescription: |
||||
Value: -11.2 |
@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2 |
||||
guid: 8c576e6a04afe7442b63e95b18254591 |
||||
NativeFormatImporter: |
||||
externalObjects: {} |
||||
mainObjectFileID: 0 |
||||
userData: |
||||
assetBundleName: |
||||
assetBundleVariant: |
@ -0,0 +1,16 @@
|
||||
%YAML 1.1 |
||||
%TAG !u! tag:unity3d.com,2011: |
||||
--- !u!114 &11400000 |
||||
MonoBehaviour: |
||||
m_ObjectHideFlags: 0 |
||||
m_CorrespondingSourceObject: {fileID: 0} |
||||
m_PrefabInstance: {fileID: 0} |
||||
m_PrefabAsset: {fileID: 0} |
||||
m_GameObject: {fileID: 0} |
||||
m_Enabled: 1 |
||||
m_EditorHideFlags: 0 |
||||
m_Script: {fileID: 11500000, guid: 7cb18c6bb3c4bcd40b8451100e11b136, type: 3} |
||||
m_Name: GravityPluto |
||||
m_EditorClassIdentifier: |
||||
DeveloperDescription: |
||||
Value: -0.5 |
@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2 |
||||
guid: 43af1b2fd533f45468473cc4650723cc |
||||
NativeFormatImporter: |
||||
externalObjects: {} |
||||
mainObjectFileID: 0 |
||||
userData: |
||||
assetBundleName: |
||||
assetBundleVariant: |
@ -0,0 +1,16 @@
|
||||
%YAML 1.1 |
||||
%TAG !u! tag:unity3d.com,2011: |
||||
--- !u!114 &11400000 |
||||
MonoBehaviour: |
||||
m_ObjectHideFlags: 0 |
||||
m_CorrespondingSourceObject: {fileID: 0} |
||||
m_PrefabInstance: {fileID: 0} |
||||
m_PrefabAsset: {fileID: 0} |
||||
m_GameObject: {fileID: 0} |
||||
m_Enabled: 1 |
||||
m_EditorHideFlags: 0 |
||||
m_Script: {fileID: 11500000, guid: 7cb18c6bb3c4bcd40b8451100e11b136, type: 3} |
||||
m_Name: GravitySaturn |
||||
m_EditorClassIdentifier: |
||||
DeveloperDescription: |
||||
Value: -10.5 |
@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2 |
||||
guid: 94ef2828596b707489700edf1e5d8256 |
||||
NativeFormatImporter: |
||||
externalObjects: {} |
||||
mainObjectFileID: 0 |
||||
userData: |
||||
assetBundleName: |
||||
assetBundleVariant: |
@ -0,0 +1,16 @@
|
||||
%YAML 1.1 |
||||
%TAG !u! tag:unity3d.com,2011: |
||||
--- !u!114 &11400000 |
||||
MonoBehaviour: |
||||
m_ObjectHideFlags: 0 |
||||
m_CorrespondingSourceObject: {fileID: 0} |
||||
m_PrefabInstance: {fileID: 0} |
||||
m_PrefabAsset: {fileID: 0} |
||||
m_GameObject: {fileID: 0} |
||||
m_Enabled: 1 |
||||
m_EditorHideFlags: 0 |
||||
m_Script: {fileID: 11500000, guid: 7cb18c6bb3c4bcd40b8451100e11b136, type: 3} |
||||
m_Name: GravityUranus |
||||
m_EditorClassIdentifier: |
||||
DeveloperDescription: |
||||
Value: -8.8 |
@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2 |
||||
guid: 51b4aa3df13827e42847b0803269b0af |
||||
NativeFormatImporter: |
||||
externalObjects: {} |
||||
mainObjectFileID: 0 |
||||
userData: |
||||
assetBundleName: |
||||
assetBundleVariant: |
@ -0,0 +1,16 @@
|
||||
%YAML 1.1 |
||||
%TAG !u! tag:unity3d.com,2011: |
||||
--- !u!114 &11400000 |
||||
MonoBehaviour: |
||||
m_ObjectHideFlags: 0 |
||||
m_CorrespondingSourceObject: {fileID: 0} |
||||
m_PrefabInstance: {fileID: 0} |
||||
m_PrefabAsset: {fileID: 0} |
||||
m_GameObject: {fileID: 0} |
||||
m_Enabled: 1 |
||||
m_EditorHideFlags: 0 |
||||
m_Script: {fileID: 11500000, guid: 7cb18c6bb3c4bcd40b8451100e11b136, type: 3} |
||||
m_Name: GravityVenus |
||||
m_EditorClassIdentifier: |
||||
DeveloperDescription: |
||||
Value: -8.8 |
@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2 |
||||
guid: 13fabc8ac191ddd4ca3971038b460d86 |
||||
NativeFormatImporter: |
||||
externalObjects: {} |
||||
mainObjectFileID: 0 |
||||
userData: |
||||
assetBundleName: |
||||
assetBundleVariant: |
@ -0,0 +1,16 @@
|
||||
%YAML 1.1 |
||||
%TAG !u! tag:unity3d.com,2011: |
||||
--- !u!114 &11400000 |
||||
MonoBehaviour: |
||||
m_ObjectHideFlags: 0 |
||||
m_CorrespondingSourceObject: {fileID: 0} |
||||
m_PrefabInstance: {fileID: 0} |
||||
m_PrefabAsset: {fileID: 0} |
||||
m_GameObject: {fileID: 0} |
||||
m_Enabled: 1 |
||||
m_EditorHideFlags: 0 |
||||
m_Script: {fileID: 11500000, guid: 7cb18c6bb3c4bcd40b8451100e11b136, type: 3} |
||||
m_Name: PlayerJumpHeight |
||||
m_EditorClassIdentifier: |
||||
DeveloperDescription: |
||||
Value: 3 |
@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2 |
||||
guid: 54e5f8576430e8b4c9d8162f5505b836 |
||||
NativeFormatImporter: |
||||
externalObjects: {} |
||||
mainObjectFileID: 0 |
||||
userData: |
||||
assetBundleName: |
||||
assetBundleVariant: |
@ -0,0 +1,16 @@
|
||||
%YAML 1.1 |
||||
%TAG !u! tag:unity3d.com,2011: |
||||
--- !u!114 &11400000 |
||||
MonoBehaviour: |
||||
m_ObjectHideFlags: 0 |
||||
m_CorrespondingSourceObject: {fileID: 0} |
||||
m_PrefabInstance: {fileID: 0} |
||||
m_PrefabAsset: {fileID: 0} |
||||
m_GameObject: {fileID: 0} |
||||
m_Enabled: 1 |
||||
m_EditorHideFlags: 0 |
||||
m_Script: {fileID: 11500000, guid: 7cb18c6bb3c4bcd40b8451100e11b136, type: 3} |
||||
m_Name: PlayerSpeed |
||||
m_EditorClassIdentifier: |
||||
DeveloperDescription: |
||||
Value: 12 |
@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2 |
||||
guid: 83bee3e171c362344adb248477e4d1ef |
||||
NativeFormatImporter: |
||||
externalObjects: {} |
||||
mainObjectFileID: 0 |
||||
userData: |
||||
assetBundleName: |
||||
assetBundleVariant: |
Loading…
Reference in new issue