movement acceleration

master
Jordan Orelli 5 years ago
parent 97b713b9fa
commit 8d667d07cb

@ -76,9 +76,9 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 7e392b35ca7959444b61a3e29a242f2b, type: 3} m_Script: {fileID: 11500000, guid: 7e392b35ca7959444b61a3e29a242f2b, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
jumpHeight: 3.5
jumpDuration: 0.4
moveSpeed: 6 moveSpeed: 6
moveController: {fileID: 0}
velocity: {x: 0, y: 0, z: 0}
--- !u!1 &6383496741659177281 --- !u!1 &6383496741659177281
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

@ -436,18 +436,7 @@ PrefabInstance:
propertyPath: m_LocalEulerAnglesHint.z propertyPath: m_LocalEulerAnglesHint.z
value: 0 value: 0
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 6891912346901077427, guid: cd445451907d7274daea79c67e13a202, m_RemovedComponents: []
type: 3}
propertyPath: collisionMask.m_Bits
value: 2048
objectReference: {fileID: 0}
- target: {fileID: 6383496741659177281, guid: cd445451907d7274daea79c67e13a202,
type: 3}
propertyPath: m_IsActive
value: 1
objectReference: {fileID: 0}
m_RemovedComponents:
- {fileID: 528243339991412957, guid: cd445451907d7274daea79c67e13a202, type: 3}
m_SourcePrefab: {fileID: 100100000, guid: cd445451907d7274daea79c67e13a202, type: 3} m_SourcePrefab: {fileID: 100100000, guid: cd445451907d7274daea79c67e13a202, type: 3}
--- !u!1001 &8206000050524585146 --- !u!1001 &8206000050524585146
PrefabInstance: PrefabInstance:

@ -130,6 +130,8 @@ public class MoveController : MonoBehaviour
verticalRaySpacing = bounds.size.x / (verticalRayCount - 1); verticalRaySpacing = bounds.size.x / (verticalRayCount - 1);
} }
public bool Grounded() { return collisions.below; }
struct RaycastOrigins { struct RaycastOrigins {
public Vector3 topBackLeft; public Vector3 topBackLeft;
public Vector3 topBackRight; public Vector3 topBackRight;

@ -4,15 +4,23 @@ using UnityEngine;
[RequireComponent(typeof(MoveController))] [RequireComponent(typeof(MoveController))]
public class PlayerController : MonoBehaviour { public class PlayerController : MonoBehaviour {
public float jumpHeight = 4f;
public float jumpDuration = 0.4f;
public float moveSpeed = 6f; public float moveSpeed = 6f;
public MoveController moveController; public float accelerationTimeAirborne = 0.2f;
public float accelerationTimeGrounded = 0.05f;
private float gravity = -20f; private Vector3 velocity;
public Vector3 velocity; private float jumpVelocity;
private MoveController moveController;
private float gravity;
private float moveXSmoothing;
void Start() { void Start() {
moveController = GetComponent<MoveController>(); moveController = GetComponent<MoveController>();
gravity = -(2*jumpHeight)/Mathf.Pow(jumpDuration, 2f);
jumpVelocity = Mathf.Abs(gravity) * jumpDuration;
} }
void Update() { void Update() {
@ -24,7 +32,12 @@ public class PlayerController : MonoBehaviour {
} }
Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")); Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
velocity.x = input.x * moveSpeed; if (Input.GetKeyDown(KeyCode.Space) && moveController.collisions.below) {
velocity.y = jumpVelocity;
}
float targetx = input.x * moveSpeed;
velocity.x = Mathf.SmoothDamp(velocity.x, targetx, ref moveXSmoothing, moveController.Grounded() ? accelerationTimeGrounded : accelerationTimeAirborne);
velocity.y += gravity * Time.deltaTime; velocity.y += gravity * Time.deltaTime;
moveController.Move(velocity * Time.deltaTime); moveController.Move(velocity * Time.deltaTime);
} }

Loading…
Cancel
Save