Platformer example started
This commit is contained in:
1 parent
efcfaae647
commit
9f026b76d6
325 files changed
+23540
-270
No files matched your search
@@ -0,0 +1,44 @@
|
||||
using System;
|
||||
using Code.Infrastructure.Configs;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Code.Example.Platformer.Entity
|
||||
{
|
||||
public abstract class Entity : MonoBehaviour
|
||||
{
|
||||
private static readonly int VelocityY = Animator.StringToHash("VelocityY");
|
||||
private static readonly int VelocityX = Animator.StringToHash("VelocityX");
|
||||
private static readonly int OnGround = Animator.StringToHash("OnGround");
|
||||
|
||||
[SerializeField] protected Rigidbody2D rigidbody2D;
|
||||
[SerializeField] protected Animator animator;
|
||||
[SerializeField] protected SpriteRenderer spriteRenderer;
|
||||
[SerializeField] private Vector2 groundPoint;
|
||||
|
||||
protected bool onGround;
|
||||
|
||||
protected virtual void Update()
|
||||
{
|
||||
animator.SetFloat(VelocityX, rigidbody2D.linearVelocityX);
|
||||
animator.SetFloat(VelocityY, rigidbody2D.linearVelocityY);
|
||||
}
|
||||
|
||||
protected virtual void HandleFlipX() => spriteRenderer.flipX = rigidbody2D.linearVelocity.x < 0.01f;
|
||||
|
||||
protected virtual void FixedUpdate()
|
||||
{
|
||||
onGround = CheckGround();
|
||||
animator.SetBool(OnGround, onGround);
|
||||
Move();
|
||||
if(rigidbody2D.linearVelocity.x is > 0.01f or < -0.01f) HandleFlipX();
|
||||
}
|
||||
|
||||
protected virtual bool CheckGround()
|
||||
{
|
||||
UnityEngine.Debug.DrawLine(transform.position,(Vector2)transform.position + groundPoint, Color.green);
|
||||
return Physics2D.OverlapPoint((Vector2)transform.position + groundPoint, LayerMasks.Masks[LayerMaskType.PlatformerGround]);
|
||||
}
|
||||
|
||||
protected abstract void Move();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user