Files
example-top-down-unity/Assets/Scripts/PlayerMovement.cs
janis 70352c3f32 + Character Sprite
+ Character animations (not moving)
# Character movement
2022-02-08 18:53:20 +01:00

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");
}
}
}