Files
harvestdale-unity/Assets/Scripts/PlayerMovement.cs
2023-02-12 16:22:41 +01:00

34 lines
753 B
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed = 5f;
private Rigidbody2D rb;
private Animator animator;
private Vector2 movement;
void Start()
{
rb = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
}
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);
}
void FixedUpdate()
{
movement.Normalize();
rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
}
}