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.

77 lines
2.5 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(MoveController))]
public class PlayerController : MonoBehaviour {
public float jumpHeight = 4f;
public float jumpDuration = 0.4f;
public float moveSpeed = 6f;
public float accelerationTimeAirborne = 0.1f;
public float accelerationTimeGrounded = 0.05f;
public Canvas hud;
public GameObject soulPrefab;
public Networking networking;
private Vector3 velocity;
private float jumpVelocity;
private MoveController moveController;
private float gravity;
private float moveXSmoothing;
private bool quitting;
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;
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);
if (other.CompareTag("Soul")) {
SoulController soul = other.GetComponent<SoulController>();
networking.SendCollectSoul(soul.playerName, other.transform.position);
Destroy(other.gameObject);
}
if (other.CompareTag("Fatal")) {
networking.SendDeath(transform.position + Vector3.up * 0.5f);
Destroy(gameObject);
}
}
void OnApplicationQuit() {
quitting = true;
}
}