mirror of
https://github.com/DerTyp7/example-top-down-unity.git
synced 2025-10-29 12:32:09 +01:00
37 lines
861 B
C#
37 lines
861 B
C#
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");
|
|
}
|
|
}
|
|
|
|
|
|
}
|