|
|
@ -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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|