Progress on movement controller

This commit is contained in:
Nicholas Novak 2023-04-17 00:01:49 -07:00
parent dfdffb9cf3
commit fb8732f820
7 changed files with 269 additions and 59 deletions

View File

@ -7,5 +7,5 @@ PhysicsMaterial2D:
m_PrefabInstance: {fileID: 0} m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_Name: PlayerFriction m_Name: PlayerFriction
friction: 1 friction: 0
bounciness: 0 bounciness: 0

View File

@ -1353,13 +1353,32 @@ Transform:
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 519420028} m_GameObject: {fileID: 519420028}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: -24.1, y: -10.43, z: -10} m_LocalPosition: {x: -25.56, y: -10.43, z: -10}
m_LocalScale: {x: 1, y: 1, z: 1} m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0 m_ConstrainProportionsScale: 0
m_Children: [] m_Children: []
m_Father: {fileID: 0} m_Father: {fileID: 0}
m_RootOrder: 0 m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &580890946 stripped
GameObject:
m_CorrespondingSourceObject: {fileID: 5885597207104481991, guid: 576d3fc87874f426294e4bbacb171478, type: 3}
m_PrefabInstance: {fileID: 1407172085}
m_PrefabAsset: {fileID: 0}
--- !u!114 &580890947
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 580890946}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 7b873819f9a3f36ef898a0403972da28, type: 3}
m_Name:
m_EditorClassIdentifier:
maxRunSpeed: 20
runAcceleration: 20
--- !u!1 &619394800 --- !u!1 &619394800
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -2366,6 +2385,14 @@ PrefabInstance:
propertyPath: m_LocalEulerAnglesHint.z propertyPath: m_LocalEulerAnglesHint.z
value: 0 value: 0
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 5885597207104481988, guid: 576d3fc87874f426294e4bbacb171478, type: 3}
propertyPath: m_Mass
value: 1
objectReference: {fileID: 0}
- target: {fileID: 5885597207104481988, guid: 576d3fc87874f426294e4bbacb171478, type: 3}
propertyPath: m_AngularDrag
value: 0
objectReference: {fileID: 0}
- target: {fileID: 5885597207104481988, guid: 576d3fc87874f426294e4bbacb171478, type: 3} - target: {fileID: 5885597207104481988, guid: 576d3fc87874f426294e4bbacb171478, type: 3}
propertyPath: m_CollisionDetection propertyPath: m_CollisionDetection
value: 1 value: 1
@ -2377,7 +2404,10 @@ PrefabInstance:
m_RemovedComponents: [] m_RemovedComponents: []
m_RemovedGameObjects: [] m_RemovedGameObjects: []
m_AddedGameObjects: [] m_AddedGameObjects: []
m_AddedComponents: [] m_AddedComponents:
- targetCorrespondingSourceObject: {fileID: 5885597207104481991, guid: 576d3fc87874f426294e4bbacb171478, type: 3}
insertIndex: -1
addedObject: {fileID: 580890947}
m_SourcePrefab: {fileID: 100100000, guid: 576d3fc87874f426294e4bbacb171478, type: 3} m_SourcePrefab: {fileID: 100100000, guid: 576d3fc87874f426294e4bbacb171478, type: 3}
--- !u!1 &1411598161 --- !u!1 &1411598161
GameObject: GameObject:

View File

@ -3,7 +3,8 @@ using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using UnityEngine.InputSystem; using UnityEngine.InputSystem;
public class PlayerBehavior : MonoBehaviour { public class PlayerBehavior : MonoBehaviour
{
[Header("Physics:")] [Header("Physics:")]
public float moveSpeed; public float moveSpeed;
@ -31,24 +32,29 @@ public class PlayerBehavior : MonoBehaviour {
private StateController stateController; private StateController stateController;
void Start() { void Start()
{
_rb = GetComponent<Rigidbody2D>(); _rb = GetComponent<Rigidbody2D>();
airSpeed = .5f * moveSpeed; airSpeed = .5f * moveSpeed;
stateController = GameObject.Find("StateController").GetComponent<StateController>(); stateController = GameObject.Find("StateController").GetComponent<StateController>();
} }
void Update() { void Update()
{
// jump // jump
// if (Input.GetKeyDown(KeyCode.Space)) { // if (Input.GetKeyDown(KeyCode.Space)) {
if (playerInput.actions["Jump"].WasPressedThisFrame() && IsGrounded()) { if (playerInput.actions["Jump"].WasPressedThisFrame() && IsGrounded())
{
_rb.AddForce(Vector2.up * jumpSpeed, ForceMode2D.Impulse); _rb.AddForce(Vector2.up * jumpSpeed, ForceMode2D.Impulse);
} }
// throw tambourine // throw tambourine
// if (Input.GetKeyDown(KeyCode.K)) { // if (Input.GetKeyDown(KeyCode.K)) {
if (playerInput.actions["ThrowTambourine"].WasPressedThisFrame()) { if (playerInput.actions["ThrowTambourine"].WasPressedThisFrame())
if (hasTambourine && !grapplingRope.isGrappling) { {
if (hasTambourine && !grapplingRope.isGrappling)
{
launcher.ThrowTambourine(forward); launcher.ThrowTambourine(forward);
hasTambourine = false; hasTambourine = false;
} }
@ -57,77 +63,99 @@ public class PlayerBehavior : MonoBehaviour {
// grapple // grapple
GameObject tambourine = GameObject.FindGameObjectWithTag("tambourine"); GameObject tambourine = GameObject.FindGameObjectWithTag("tambourine");
// if (Input.GetKeyDown(KeyCode.L)) { // if (Input.GetKeyDown(KeyCode.L)) {
if (playerInput.actions["Grapple"].WasPressedThisFrame()) { if (playerInput.actions["Grapple"].WasPressedThisFrame())
if (tambourine != null) { // grapple to tambourine {
if (!grapplingRope.isGrappling && tambourine.GetComponent<TambourineBehavior>().pinned) { if (tambourine != null)
{ // grapple to tambourine
if (!grapplingRope.isGrappling && tambourine.GetComponent<TambourineBehavior>().pinned)
{
grapplingGun.GrappleToTambourine(tambourine); grapplingGun.GrappleToTambourine(tambourine);
grapplingRope.isGrappling = true; grapplingRope.isGrappling = true;
} }
} else { }
if (grappleSurface != null) { else
{
if (grappleSurface != null)
{
grapplingGun.GrappleToSurface(grappleSurface.transform.position); grapplingGun.GrappleToSurface(grappleSurface.transform.position);
grapplingRope.isGrappling = true; grapplingRope.isGrappling = true;
} }
} }
} }
// if (Input.GetKeyUp(KeyCode.L)) { // if (Input.GetKeyUp(KeyCode.L)) {
if (playerInput.actions["Grapple"].WasReleasedThisFrame()) { if (playerInput.actions["Grapple"].WasReleasedThisFrame())
if (tambourine != null && grapplingRope.isGrappling) { {
if (tambourine != null && grapplingRope.isGrappling)
{
tambourine.GetComponent<TambourineBehavior>().DestroySelf(); tambourine.GetComponent<TambourineBehavior>().DestroySelf();
} }
grapplingGun.ReleaseGrapple(); grapplingGun.ReleaseGrapple();
} }
// if (Input.GetKey(KeyCode.L)) { // if (Input.GetKey(KeyCode.L)) {
if (playerInput.actions["Grapple"].IsPressed()) { if (playerInput.actions["Grapple"].IsPressed())
{
Debug.DrawRay(transform.position, new Vector2(0.500f * forward, 0.866f), Color.green); Debug.DrawRay(transform.position, new Vector2(0.500f * forward, 0.866f), Color.green);
} }
} }
void OnMove(InputValue value) { void OnMove(InputValue value)
{
_hInput = value.Get<Vector2>().x; _hInput = value.Get<Vector2>().x;
if (_hInput < 0) { if (_hInput < 0)
{
forward = -1; forward = -1;
} else if (_hInput > 0) { }
else if (_hInput > 0)
{
forward = 1; forward = 1;
} }
} }
void FixedUpdate() { // void FixedUpdate() {
if (grapplingRope.isGrappling && _hInput != 0 && !IsGrounded()) { // if (grapplingRope.isGrappling && _hInput != 0 && !IsGrounded()) {
// print("grappling force"); // // print("grappling force");
_rb.AddForce(new Vector2(_hInput * (airSpeed / 3), 0)); // _rb.AddForce(new Vector2(_hInput * (airSpeed / 3), 0));
} else if (_hInput != 0 && !IsGrounded()) { // } else if (_hInput != 0 && !IsGrounded()) {
_rb.AddForce(new Vector2(_hInput * airSpeed, 0)); // _rb.AddForce(new Vector2(_hInput * airSpeed, 0));
} else if (_hInput != 0) { // } else if (_hInput != 0) {
// print("normal movement"); // // print("normal movement");
_rb.AddForce(new Vector2(_hInput * moveSpeed, 0)); // _rb.AddForce(new Vector2(_hInput * moveSpeed, 0));
// _rb.velocity = new Vector2(_hInput * moveSpeed, _rb.velocity.y); // // _rb.velocity = new Vector2(_hInput * moveSpeed, _rb.velocity.y);
} // }
} // }
void OnTriggerEnter2D(Collider2D col) { void OnTriggerEnter2D(Collider2D col)
if (col.tag == "grappleSurface") { {
if (col.tag == "grappleSurface")
{
grappleSurface = col.gameObject; grappleSurface = col.gameObject;
} else if (col.tag == "instaDeath") { }
else if (col.tag == "instaDeath")
{
stateController.ToggleDeathCanvas(); stateController.ToggleDeathCanvas();
Destroy(this.gameObject); Destroy(this.gameObject);
} }
} }
void OnTriggerExit2D(Collider2D col) { void OnTriggerExit2D(Collider2D col)
if (col.tag == "grappleSurface") { {
if (col.tag == "grappleSurface")
{
grappleSurface = null; grappleSurface = null;
} }
} }
bool IsGrounded() { bool IsGrounded()
if (Physics2D.BoxCast(transform.position, boxSize, 0, -transform.up, maxDistanceFromGround, groundLayer)) { {
if (Physics2D.BoxCast(transform.position, boxSize, 0, -transform.up, maxDistanceFromGround, groundLayer))
{
return true; return true;
} }
return false; return false;
} }
void OnDrawGizmos() { void OnDrawGizmos()
{
Gizmos.color = Color.red; Gizmos.color = Color.red;
Gizmos.DrawCube(transform.position - transform.up * maxDistanceFromGround, boxSize); Gizmos.DrawCube(transform.position - transform.up * maxDistanceFromGround, boxSize);
} }

View File

@ -1,31 +1,74 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using UnityEngine.InputSystem; using UnityEngine.InputSystem;
public class PlayerController : MonoBehaviour public class PlayerMovement : MonoBehaviour
{ {
private Rigidbody2D rb;
Vector2 movementValue; public float maxRunSpeed;
[SerializeField] public float runAcceleration;
float speed; private bool onGround = false;
float hangTimeThreshold = 0.1f;
float hangTimeAccel = 0;
float hangTimeSpeed = 0;
private Vector2 movement = Vector2.zero;
void OnValidate()
{
this.runAcceleration = Mathf.Clamp(runAcceleration, 0.1f, this.maxRunSpeed);
}
[SerializeField]
Rigidbody2D rb;
// Start is called before the first frame update
void Start() void Start()
{ {
this.rb = this.GetComponent<Rigidbody2D>();
} }
void OnMove(InputValue value) void OnMove(InputValue value)
{ {
this.movementValue = value.Get<Vector2>() * speed; this.movement = value.Get<Vector2>();
//Debug.Log(this.movement);
} }
// Update is called once per frame void FixedUpdate()
void Update()
{ {
this.rb.AddForce(new Vector3(this.movementValue.x, 0, this.movementValue.y)); Run(1);
}
float AccelerationRate()
{
return this.runAcceleration / this.maxRunSpeed;
}
private void Run(float lerpAmount)
{
float targetSpeed = this.movement.x * this.maxRunSpeed;
float speedDiff = targetSpeed - this.rb.velocity.x;
float accel = 0.5f;
float accelRate = (Mathf.Abs(targetSpeed) > 0.1) ? accel : -accel;
float velPower = 1.0f;
float move = Mathf.Pow(Mathf.Abs(speedDiff) * accelRate, velPower) * Mathf.Sign(speedDiff);
this.rb.AddForce(move * Vector2.right, ForceMode2D.Force);
float frictionAmount = 0.5f;
this.onGround = true;
if (this.onGround && (Mathf.Abs(this.movement.x) < 0.1f))
{
float amount = Mathf.Min(
Mathf.Abs(this.rb.velocity.x),
Mathf.Abs(frictionAmount)
);
amount *= Mathf.Sign(this.rb.velocity.x);
this.rb.AddForce(-amount * Vector2.right, ForceMode2D.Impulse);
}
} }
} }

View File

@ -0,0 +1,98 @@
using UnityEngine;
[CreateAssetMenu(menuName = "Player Data")] //Create a new playerData object by right clicking in the Project Menu then Create/Player/Player Data and drag onto the player
public class PlayerData : ScriptableObject
{
[Header("Gravity")]
[HideInInspector]
public float gravityStrength; //Downwards force (gravity) needed for the desired jumpHeight and jumpTimeToApex.
[HideInInspector]
public float gravityScale; //Strength of the player's gravity as a multiplier of gravity (set in ProjectSettings/Physics2D).
//Also the value the player's rigidbody2D.gravityScale is set to.
[Space(5)]
public float fallGravityMult; //Multiplier to the player's gravityScale when falling.
public float maxFallSpeed; //Maximum fall speed (terminal velocity) of the player when falling.
[Space(5)]
public float fastFallGravityMult; //Larger multiplier to the player's gravityScale when they are falling and a downwards input is pressed.
//Seen in games such as Celeste, lets the player fall extra fast if they wish.
public float maxFastFallSpeed; //Maximum fall speed(terminal velocity) of the player when performing a faster fall.
[Space(20)]
[Header("Run")]
public float runMaxSpeed; //Target speed we want the player to reach.
public float runAcceleration; //The speed at which our player accelerates to max speed, can be set to runMaxSpeed for instant acceleration down to 0 for none at all
[HideInInspector]
public float runAccelAmount; //The actual force (multiplied with speedDiff) applied to the player.
public float runDecceleration; //The speed at which our player decelerates from their current speed, can be set to runMaxSpeed for instant deceleration down to 0 for none at all
[HideInInspector]
public float runDeccelAmount; //Actual force (multiplied with speedDiff) applied to the player .
[Space(5)]
[Range(0f, 1)]
public float accelInAir; //Multipliers applied to acceleration rate when airborne.
[Range(0f, 1)]
public float deccelInAir;
[Space(5)]
public bool doConserveMomentum = true;
[Space(20)]
[Header("Jump")]
public float jumpHeight; //Height of the player's jump
public float jumpTimeToApex; //Time between applying the jump force and reaching the desired jump height. These values also control the player's gravity and jump force.
[HideInInspector]
public float jumpForce; //The actual force applied (upwards) to the player when they jump.
[Header("Both Jumps")]
public float jumpCutGravityMult; //Multiplier to increase gravity if the player releases thje jump button while still jumping
[Range(0f, 1)]
public float jumpHangGravityMult; //Reduces gravity while close to the apex (desired max height) of the jump
public float jumpHangTimeThreshold; //Speeds (close to 0) where the player will experience extra "jump hang". The player's velocity.y is closest to 0 at the jump's apex (think of the gradient of a parabola or quadratic function)
[Space(0.5f)]
public float jumpHangAccelerationMult;
public float jumpHangMaxSpeedMult;
[Header("Wall Jump")]
public Vector2 wallJumpForce; //The actual force (this time set by us) applied to the player when wall jumping.
[Space(5)]
[Range(0f, 1f)]
public float wallJumpRunLerp; //Reduces the effect of player's movement while wall jumping.
[Range(0f, 1.5f)]
public float wallJumpTime; //Time after wall jumping the player's movement is slowed for.
public bool doTurnOnWallJump; //Player will rotate to face wall jumping direction
[Space(20)]
[Header("Slide")]
public float slideSpeed;
public float slideAccel;
[Header("Assists")]
[Range(0.01f, 0.5f)]
public float coyoteTime; //Grace period after falling off a platform, where you can still jump
[Range(0.01f, 0.5f)]
public float jumpInputBufferTime; //Grace period after pressing jump where a jump will be automatically performed once the requirements (eg. being grounded) are met.
//Unity Callback, called when the inspector updates
private void OnValidate()
{
//Calculate gravity strength using the formula (gravity = 2 * jumpHeight / timeToJumpApex^2)
gravityStrength = -(2 * jumpHeight) / (jumpTimeToApex * jumpTimeToApex);
//Calculate the rigidbody's gravity scale (ie: gravity strength relative to unity's gravity value, see project settings/Physics2D)
gravityScale = gravityStrength / Physics2D.gravity.y;
//Calculate are run acceleration & deceleration forces using formula: amount = ((1 / Time.fixedDeltaTime) * acceleration) / runMaxSpeed
runAccelAmount = (50 * runAcceleration) / runMaxSpeed;
runDeccelAmount = (50 * runDecceleration) / runMaxSpeed;
//Calculate jumpForce using the formula (initialJumpVelocity = gravity * timeToJumpApex)
jumpForce = Mathf.Abs(gravityStrength) * jumpTimeToApex;
#region Variable Ranges
runAcceleration = Mathf.Clamp(runAcceleration, 0.01f, runMaxSpeed);
runDecceleration = Mathf.Clamp(runDecceleration, 0.01f, runMaxSpeed);
#endregion
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 5ddb4766145463c75a48eeb29e77e0d3
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

@ -1 +1 @@
Subproject commit 413bfb2c6937c2d11546a7a81aed5ae43d5cc147 Subproject commit 23938e9b94b9022413054828a87c05d8f5decc2b