Start of character animations

This commit is contained in:
Simon Lübeß
2024-04-08 12:00:48 +02:00
parent e4841fe3fc
commit 8c1bb33bdf
83 changed files with 64508 additions and 581 deletions

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using Data;
using Interaction;
using TMPro.Examples;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
@ -12,31 +13,30 @@ using Utility;
public class Character : MonoBehaviour
{
[Header("Movement")]
public float moveSpeed = 2f;
[SerializeField] private Animator _animator;
[FormerlySerializedAs("moveSpeed")] [Header("Movement")]
public float _movementAcceleration = 2f;
public float groundDrag = 1f;
private float horizontalInput, verticalInput;
private Vector3 moveDirection;
private Rigidbody rb;
public float speed = 2f;
[FormerlySerializedAs("speed")] public float _maxVelocity = 2f;
[Header("Turning")]
public float turningRate = 30f;
public Quaternion _targetRotation = Quaternion.identity;
[Header("Keybindings")]
public KeyCode jumpKey = KeyCode.Space;
[Header("Jumping")]
public float jumpForce;
public float jumpCooldown;
bool readyToJump = true;
private bool _readyToJump = true;
[Header("Groud Check")]
public float playerHeight;
public LayerMask whatIsGround;
public bool grounded;
[SerializeField]private float _groundedTolerance = 0.2f;
[SerializeField]private LayerMask _groundLayerMask;
private bool _grounded;
[Header("Hold item")]
[SerializeField] private Transform _hand;
@ -50,7 +50,11 @@ public class Character : MonoBehaviour
[SerializeField]
private AudioSource _audioSource;
private static readonly int RunSpeed = Animator.StringToHash("RunSpeed");
private static readonly int IsFalling = Animator.StringToHash("IsFalling");
private static readonly int IsJumping = Animator.StringToHash("IsJumping");
public bool CarriesItem => _carriedInteractible;
public PickupInteractible CarriedItem => _carriedInteractible;
@ -64,13 +68,13 @@ public class Character : MonoBehaviour
// Update is called once per frame
void Update()
{
grounded = Physics.Raycast(transform.position, Vector3.down, playerHeight * 0.5f + 0.2f, whatIsGround);
_grounded = Physics.Raycast(transform.position + new Vector3(0, _groundedTolerance / 2.0f, 0), Vector3.down, _groundedTolerance, _groundLayerMask);
SpeedControl();
transform.rotation = Quaternion.RotateTowards(transform.rotation, _targetRotation, turningRate * Time.deltaTime);
if (grounded)
if (_grounded)
{
rb.drag = groundDrag;
}
@ -79,6 +83,24 @@ public class Character : MonoBehaviour
rb.drag = 0.3f;
}
UpdateAnimator();
}
private void UpdateAnimator()
{
//if (_grounded)
{
Vector3 velocity = rb.velocity;
velocity.y = 0.0f;
_animator.SetFloat(RunSpeed, velocity.magnitude);
}
//else
{
//_animator.SetFloat(RunSpeed, 0);
}
_animator.SetBool(IsFalling, !_grounded);
}
public void PickupItem(PickupInteractible itemToPickup)
@ -122,18 +144,17 @@ public class Character : MonoBehaviour
{
Vector2 movement = context.ReadValue<Vector2>();
horizontalInput = movement.x;
verticalInput = movement.y;
if(horizontalInput != 0f || verticalInput != 0f)
moveDirection = new Vector3(movement.x, 0, movement.y);
if(moveDirection.x != 0f || moveDirection.z != 0f)
{
_targetRotation = Quaternion.Euler(new Vector3(0f, Mathf.Atan2(horizontalInput, verticalInput) * Mathf.Rad2Deg, 0f));
_targetRotation = Quaternion.Euler(new Vector3(0f, Mathf.Atan2(moveDirection.x, moveDirection.z) * Mathf.Rad2Deg, 0f));
}
}
public void DoJump(InputAction.CallbackContext context)
{
if (!context.performed || !readyToJump || !grounded)
if (!context.performed || !_readyToJump || !_grounded)
return;
Jump();
@ -151,24 +172,24 @@ public class Character : MonoBehaviour
private void MovePlayer()
{
moveDirection = new Vector3(0, 0, verticalInput) + new Vector3(horizontalInput, 0, 0);
rb.AddForce(moveDirection.normalized * moveSpeed * 10f, ForceMode.Force);
rb.AddForce(moveDirection.normalized * _movementAcceleration, ForceMode.Acceleration);
}
private void SpeedControl()
{
Vector3 flatVel = new Vector3(rb.velocity.x, 0f, rb.velocity.z);
if (flatVel.magnitude > moveSpeed)
if (flatVel.magnitude > _maxVelocity)
{
Vector3 limitedVel = flatVel.normalized * moveSpeed;
Vector3 limitedVel = flatVel.normalized * _maxVelocity;
rb.velocity = new Vector3(limitedVel.x, rb.velocity.y, limitedVel.z);
}
}
private void Jump()
{
_animator.SetTrigger(IsJumping);
rb.velocity = new Vector3(rb.velocity.x, 0f, rb.velocity.z);
rb.AddForce(transform.up * jumpForce, ForceMode.Impulse);
@ -176,7 +197,7 @@ public class Character : MonoBehaviour
private void ResetJump()
{
readyToJump = true;
_readyToJump = true;
}
public void GiveItemTo(Developer developer)