You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
57 lines
1.4 KiB
57 lines
1.4 KiB
using UnityEngine; |
|
|
|
public class PlayerController : MonoBehaviour |
|
{ |
|
[SerializeField] private int speed; |
|
[SerializeField] private Animator anim; |
|
[SerializeField] private SpriteRenderer playerSprite; |
|
|
|
private PlayerControls playerControls; |
|
private Rigidbody rb; |
|
private Vector3 movement; |
|
|
|
private const string IS_WALK_PARAM = "IsWalk"; |
|
|
|
// Start is called once before the first execution of Update after the MonoBehaviour is created |
|
private void Awake() |
|
{ |
|
playerControls = new PlayerControls(); |
|
} |
|
|
|
private void OnEnable() |
|
{ |
|
playerControls.Enable(); |
|
} |
|
|
|
private void Start() |
|
{ |
|
rb = GetComponent<Rigidbody>(); |
|
} |
|
|
|
// Update is called once per frame |
|
private void Update() |
|
{ |
|
float x = playerControls.Player.Move.ReadValue<Vector2>().x; |
|
float z = playerControls.Player.Move.ReadValue<Vector2>().y; |
|
|
|
movement = new Vector3(x, 0, z).normalized; |
|
|
|
anim.SetBool(IS_WALK_PARAM, movement != Vector3.zero); |
|
if(x != 0 && x < 0) |
|
{ |
|
playerSprite.flipX = true; |
|
} |
|
if(x != 0 && x > 0) |
|
{ |
|
playerSprite.flipX = false; |
|
} |
|
} |
|
|
|
private void FixedUpdate() |
|
{ |
|
if (movement != null) |
|
{ |
|
rb.MovePosition(transform.position + movement * speed * Time.fixedDeltaTime); |
|
} |
|
} |
|
}
|
|
|