Browse Source

Working on running

master
Ken Schaefer 5 years ago
parent
commit
bda6ed09ce
  1. 80
      Assets/FirstPersonController/Scripts/PlayerMovement.cs
  2. 16
      Assets/FirstPersonController/Variables/PlayerRunSpeed.asset
  3. 8
      Assets/FirstPersonController/Variables/PlayerRunSpeed.asset.meta
  4. 2
      Assets/FirstPersonController/Variables/PlayerWalkSpeed.asset
  5. 0
      Assets/FirstPersonController/Variables/PlayerWalkSpeed.asset.meta

80
Assets/FirstPersonController/Scripts/PlayerMovement.cs

@ -19,47 +19,57 @@ using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
[Header("Character")]
[SerializeField] private CharacterController controller;
[Header("Character")]
[SerializeField] private CharacterController controller;
[Header("Attributes")]
[SerializeField] private FloatReference playerSpeed;
[SerializeField] private FloatReference jumpHeight;
[Header("Attributes")]
[SerializeField] private FloatReference playerWalkSpeed;
[SerializeField] private FloatReference playerRunSpeed;
[SerializeField] private FloatReference jumpHeight;
[Header("Environment")]
[SerializeField] private FloatReference gravity;
private Vector3 velocity;
[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;
[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;
}
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");
// 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);
Vector3 move = transform.right * x + transform.forward * z;
if (Input.GetKeyDown(KeyCode.LeftShift) && isGrounded)
{
Debug.Log("Running");
controller.Move(move * playerRunSpeed * Time.deltaTime);
}
else
{
Debug.Log("Walking");
controller.Move(move * playerWalkSpeed.Value * Time.deltaTime);
}
// Implement jumping
if(Input.GetButtonDown("Jump") && isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight.Value * -2f * gravity.Value);
}
// 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);
}
// Implement gravity
velocity.y += gravity.Value * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
}

16
Assets/FirstPersonController/Variables/PlayerRunSpeed.asset

@ -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: PlayerRunSpeed
m_EditorClassIdentifier:
DeveloperDescription:
Value: 36

8
Assets/FirstPersonController/Variables/PlayerRunSpeed.asset.meta

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: e96483bba2736bb499237d1aa26f580a
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 0
userData:
assetBundleName:
assetBundleVariant:

2
Assets/FirstPersonController/Variables/PlayerSpeed.asset → Assets/FirstPersonController/Variables/PlayerWalkSpeed.asset

@ -10,7 +10,7 @@ MonoBehaviour:
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 7cb18c6bb3c4bcd40b8451100e11b136, type: 3}
m_Name: PlayerSpeed
m_Name: PlayerWalkSpeed
m_EditorClassIdentifier:
DeveloperDescription:
Value: 12

0
Assets/FirstPersonController/Variables/PlayerSpeed.asset.meta → Assets/FirstPersonController/Variables/PlayerWalkSpeed.asset.meta

Loading…
Cancel
Save