You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
2.0 KiB
C#
61 lines
2.0 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
[RequireComponent(typeof(MoveController))]
|
|
public class PlayerController : MonoBehaviour {
|
|
|
|
public float jumpHeight = 5f;
|
|
public float jumpDuration = 0.4f;
|
|
public float moveSpeed = 6f;
|
|
public float accelerationTimeAirborne = 0.1f;
|
|
public float accelerationTimeGrounded = 0.05f;
|
|
|
|
private Vector3 velocity;
|
|
private float jumpVelocity;
|
|
private MoveController moveController;
|
|
private float gravity;
|
|
private float moveXSmoothing;
|
|
|
|
void Start() {
|
|
moveController = GetComponent<MoveController>();
|
|
gravity = -(2*jumpHeight)/Mathf.Pow(jumpDuration, 2f);
|
|
jumpVelocity = Mathf.Abs(gravity) * jumpDuration;
|
|
}
|
|
|
|
void Update() {
|
|
if (moveController.collisions.above || moveController.collisions.below) {
|
|
velocity.y = 0;
|
|
}
|
|
if (moveController.collisions.left || moveController.collisions.right) {
|
|
velocity.x = 0;
|
|
}
|
|
|
|
Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
|
|
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;
|
|
Debug.LogFormat("Grounded: {0}", moveController.Grounded());
|
|
moveController.Move(velocity * Time.deltaTime);
|
|
}
|
|
|
|
void FixedUpdate() {
|
|
}
|
|
|
|
void OnDestroy() {
|
|
Debug.Log("I'm dead");
|
|
}
|
|
|
|
void OnCollisionEnter(Collision other) {
|
|
// Debug.LogFormat("Player collided with {0}", other);
|
|
}
|
|
|
|
void OnTriggerEnter(Collider other) {
|
|
// Debug.LogFormat("Player triggered other: {0}", other);
|
|
}
|
|
}
|