This repository has been archived on 2024-11-28. You can view files and clone it, but cannot push or open issues or pull requests.
PMC_Projekt/ProjektUnity/Assets/Scripts/Player/PlayerMover.cs
2024-09-06 11:38:44 +02:00

61 lines
1.1 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//Ben Keller
public class PlayerMover : MonoBehaviour
{
public KeyCode up = KeyCode.W;
public KeyCode down = KeyCode.S;
public KeyCode left = KeyCode.A;
public KeyCode right = KeyCode.D;
public Rigidbody2D rb;
public PlayerStats playerStats;
public float speed = 2f;
public Player player;
void Start()
{
rb = GetComponent<Rigidbody2D>();
playerStats = GetComponent<PlayerStats>();
speed = playerStats.speed;
}
void Update()
{
Move();
player.playerPosition = transform.position;
}
public void Move(){
Vector2 moveDirection = Vector2.zero;
if(Input.GetKey(up)){
moveDirection += Vector2.up;
}
if(Input.GetKey(down)){
moveDirection += Vector2.down;
}
if(Input.GetKey(left)){
moveDirection += Vector2.left;
}
if(Input.GetKey(right)){
moveDirection += Vector2.right;
}
if(moveDirection.magnitude > 1){
moveDirection.Normalize();
}
rb.velocity = moveDirection * speed;
}
}