Player Movements
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
using UnityEngine;
|
||||
|
||||
[RequireComponent(typeof(SpriteRenderer)), RequireComponent(typeof(Animator))]
|
||||
public class PlayerMovements : MonoBehaviour
|
||||
{
|
||||
private readonly float _speed = .15f;
|
||||
|
||||
private SpriteRenderer _sr;
|
||||
private Animator _animator;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
_sr = GetComponent<SpriteRenderer>();
|
||||
_animator = GetComponent<Animator>();
|
||||
}
|
||||
|
||||
private void FixedUpdate()
|
||||
{
|
||||
/** --- Move player along 2D axis ----------------------------------*/
|
||||
float moveH = Input.GetAxis("Horizontal") * _speed;
|
||||
float moveV = Input.GetAxis("Vertical") * _speed;
|
||||
|
||||
transform.Translate(new Vector2(moveH, moveV));
|
||||
|
||||
/** --- Rotate sprite ----------------------------------------------*/
|
||||
if (moveH < 0)
|
||||
{
|
||||
_sr.flipX = true;
|
||||
} else if (moveH > 0)
|
||||
{
|
||||
_sr.flipX = false;
|
||||
}
|
||||
|
||||
/** --- update animator -------------------------------------------*/
|
||||
_animator.SetFloat("Speed", Mathf.Max(Mathf.Abs(moveH), Mathf.Abs(moveV)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 32f5e7611866dce4ab320c814d279d84
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user