mirror of
https://github.com/DerTyp7/example-top-down-unity.git
synced 2025-10-31 21:27:07 +01:00
Interaction
This commit is contained in:
@@ -2,25 +2,37 @@ 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;
|
||||
|
||||
[SerializeField] Rigidbody2D rb;
|
||||
[SerializeField] Animator animator;
|
||||
|
||||
Rigidbody2D rb;
|
||||
Animator animator;
|
||||
Vector2 movement;
|
||||
private void Update()
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
private void FixedUpdate()
|
||||
void FixedUpdate()
|
||||
{
|
||||
if (rb != null)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user