Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.SceneManagement;
- public class Player : MonoBehaviour
- {
- private CharacterController _controller;
- private PlayerControls _playerInputs;
- [SerializeField]
- private float _moveSpeed = 5f;
- [SerializeField]
- private float _gravity = 10f;
- [SerializeField]
- private float _jumpHeight = 50f;
- private bool _isJumping;
- private bool _doubleJumped = false;
- [SerializeField]
- private float _yVelocity;
- private bool _isGrounded;
- [SerializeField]
- private int _score = 0;
- [SerializeField]
- private int _lives = 3;
- [SerializeField]
- private float _respawnDepth = -20f;
- [SerializeField]
- private Transform _respawnPoint;
- [SerializeField]
- private float _airControlMult = 0.1f;
- public delegate void OnInteract();
- public static event OnInteract onInteract;
- // Start is called before the first frame update
- void Start()
- {
- if (!TryGetComponent<CharacterController>(out _controller))
- {
- Debug.LogError("No character controller defined");
- Debug.Break();
- }
- _controller.enabled = false;
- transform.position = _respawnPoint.position;
- _controller.enabled = true;
- _playerInputs = new PlayerControls();
- _playerInputs.Platforming.Enable();
- _playerInputs.Platforming.Jump.performed += Jump_performed;
- _playerInputs.Platforming.Interact.performed += Interact_performed;
- if (_respawnPoint == null)
- {
- Debug.Log("No respawn point set, assuming initial position");
- _respawnPoint = transform;
- }
- }
- private void Interact_performed(UnityEngine.InputSystem.InputAction.CallbackContext obj)
- {
- onInteract.Invoke();
- }
- public int GetScore() {
- return _score;
- }
- private void OnEnable()
- {
- Collectible.onCollect += Collectible_onCollect;
- }
- private void OnDisable()
- {
- Collectible.onCollect -= Collectible_onCollect;
- }
- private void OnCollisionEnter(Collision collision)
- {
- Debug.Log("Collision Enter");
- }
- private void OnCollisionStay(Collision collision)
- {
- Debug.Log("Collided");
- if (!_isGrounded && collision.gameObject.CompareTag("Wall"))
- {
- ContactPoint[] contactPoints = new ContactPoint[collision.contactCount];
- int collisionCount = collision.GetContacts(contactPoints);
- foreach (ContactPoint cp in contactPoints)
- {
- Debug.DrawRay(cp.point, cp.normal);
- }
- }
- }
- private void OnControllerColliderHit(ControllerColliderHit hit)
- {
- if (!_isGrounded)
- {
- Debug.DrawRay(hit.point, hit.normal * 5, Color.green);
- }
- }
- private void Collectible_onCollect(int value)
- {
- _score += value;
- UIManager.Instance.UpdateScore(_score);
- }
- private void Jump_performed(UnityEngine.InputSystem.InputAction.CallbackContext obj)
- {
- if (_isGrounded || !_doubleJumped)
- {
- _isJumping = true;
- }
- }
- private void FixedUpdate()
- {
- _isGrounded = _controller.isGrounded;
- }
- // Update is called once per frame
- void Update()
- {
- Vector2 playerMovement = _playerInputs.Platforming.Movement.ReadValue<Vector2>();
- Vector3 velocity = _controller.velocity;
- if (_isGrounded)
- {
- velocity = new Vector3(playerMovement.x, 0, 0) * _moveSpeed;
- }
- if (!_isGrounded)
- {
- if (_isJumping && !_doubleJumped)
- {
- _yVelocity += _jumpHeight;
- _doubleJumped = true;
- _isJumping = false;
- }
- _yVelocity -= _gravity;
- }
- else
- {
- if (_isJumping)
- {
- _yVelocity = _jumpHeight;
- _isJumping = false;
- _doubleJumped = false;
- }
- else
- {
- // _yVelocity = 0;
- }
- }
- velocity.y = _yVelocity;
- if (velocity.z > 0)
- {
- Debug.Log(velocity);
- Debug.Break();
- }
- _controller.Move(velocity * Time.deltaTime);
- if (transform.position.y < _respawnDepth)
- {
- Debug.Log("Respawning");
- // Character has fallen and died
- _lives--;
- UIManager.Instance.UpdateLives(_lives);
- if (_lives <= 0)
- {
- // Game Over
- SceneManager.LoadScene("Level1");
- }
- _controller.enabled = false;
- transform.position = _respawnPoint.position;
- _controller.enabled = true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement