Ruch za pomocą Translate
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
[SerializeField] float speed = 5f;
void Update()
{
MovePlayer();
}
void MovePlayer()
{
Vector2 movement = new(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
movement.Normalize();
transform.Translate(Time.deltaTime * speed * movement);
}
}
Ruch za pomocą rb
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovementRb : MonoBehaviour
{
[SerializeField] float speed = 5f;
Rigidbody2D rb;
Vector2 movement;
private void Awake()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
movement = new(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
}
private void FixedUpdate()
{
movement.Normalize();
rb.velocity = movement * speed;
}
}