mirror of
https://github.com/DerTyp7/example-top-down-unity.git
synced 2025-10-30 21:07:09 +01:00
+ Character Sprite
+ Character animations (not moving) # Character movement
This commit is contained in:
36
Assets/Scripts/PlayerMovement.cs
Normal file
36
Assets/Scripts/PlayerMovement.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class PlayerMovement : MonoBehaviour
|
||||
{
|
||||
[SerializeField] float moveSpeed = 5f;
|
||||
|
||||
[SerializeField] Rigidbody2D rb;
|
||||
[SerializeField] Animator animator;
|
||||
|
||||
Vector2 movement;
|
||||
private void Update()
|
||||
{
|
||||
movement.x = Input.GetAxisRaw("Horizontal");
|
||||
movement.y = Input.GetAxisRaw("Vertical");
|
||||
|
||||
animator.SetFloat("Horizontal", movement.x);
|
||||
animator.SetFloat("Vertical", movement.y);
|
||||
animator.SetFloat("Speed", movement.sqrMagnitude);
|
||||
}
|
||||
|
||||
private void FixedUpdate()
|
||||
{
|
||||
if (rb != null)
|
||||
{
|
||||
rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("No Rigidbody2D found in PlayerMovement.cs");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user