mirror of
https://github.com/DerTyp7/example-top-down-unity.git
synced 2025-10-29 12:32:09 +01:00
49 lines
1.1 KiB
C#
49 lines
1.1 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
// Handles the player's movement, such as running, walking, sneaking
|
|
public class PlayerMovement : MonoBehaviour
|
|
{
|
|
// Speed multiplier of the movement
|
|
[SerializeField] float moveSpeed = 5f;
|
|
|
|
|
|
Rigidbody2D rb;
|
|
Animator animator;
|
|
Vector2 movement;
|
|
|
|
void Start()
|
|
{
|
|
rb = GetComponent<Rigidbody2D>();
|
|
animator = GetComponent<Animator>();
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
movement.x = Input.GetAxisRaw("Horizontal");
|
|
movement.y = Input.GetAxisRaw("Vertical");
|
|
|
|
// Normalize, so the player is not faster by moving diagonally
|
|
movement.Normalize();
|
|
|
|
animator.SetFloat("Horizontal", movement.x);
|
|
animator.SetFloat("Vertical", movement.y);
|
|
animator.SetFloat("Speed", movement.sqrMagnitude);
|
|
}
|
|
|
|
void FixedUpdate()
|
|
{
|
|
if (rb != null)
|
|
{
|
|
rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError("No Rigidbody2D found in PlayerMovement.cs");
|
|
}
|
|
}
|
|
|
|
|
|
}
|