112 lines
3.3 KiB
C#
112 lines
3.3 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Unity.VisualScripting;
|
|
using UnityEngine;
|
|
using TMPro;
|
|
|
|
public class PlayerMovement : MonoBehaviour
|
|
{
|
|
public float score = 0f;
|
|
public Vector3 moveVector;
|
|
public GameObject character;
|
|
private Rigidbody2D rb;
|
|
public GameObject uiElement;
|
|
public TextMeshProUGUI scoreText;
|
|
public TextMeshProUGUI highscoreText;
|
|
[SerializeField] private float minXCoordinate = -8.0f;
|
|
[SerializeField] private float maxXCoordinate = 8.0f;
|
|
[SerializeField] private float minYCoordinate = -5.0f;
|
|
[SerializeField] private float maxYCoordinate = 5.0f;
|
|
[SerializeField] private float horizontalSpeed = 5;
|
|
[SerializeField] private float verticalSpeed = 5;
|
|
|
|
void Start()
|
|
{
|
|
rb = GetComponent<Rigidbody2D>();
|
|
moveVector = Vector3.zero;
|
|
highscoreText.text = PlayerPrefs.GetFloat("HighScore").ToString();
|
|
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
handleMovement();
|
|
ScorePanel();
|
|
}
|
|
|
|
private void handleMovement()
|
|
{
|
|
// Aufgabe:
|
|
// 1. Abfragen der Vertical Axis
|
|
// 2. Wert in movementDirection setzen
|
|
// 3. Neue Grenzen für oben und unten anlegen
|
|
// 4. Position des Spieler auf neue Grenzen testen
|
|
// und ggf. zurücksetzen
|
|
|
|
Vector3 movementDirection;
|
|
|
|
|
|
float deltaX = Input.GetAxis("Horizontal"); // Vertical
|
|
float deltaY = Input.GetAxis("Vertical"); // Vertical
|
|
|
|
movementDirection = new Vector3(deltaX, 0, 0);
|
|
// x-movement
|
|
transform.position += movementDirection * horizontalSpeed * Time.deltaTime;
|
|
// Alternativ: transform.Translate(movementDirection * horizontalSpeed * Time.deltaTime);
|
|
|
|
if (transform.position.x < minXCoordinate)
|
|
{
|
|
transform.position = new Vector3(minXCoordinate, transform.position.y, transform.position.z);
|
|
}
|
|
else if (transform.position.x > maxXCoordinate)
|
|
{
|
|
transform.position = new Vector3(maxXCoordinate, transform.position.y, transform.position.z);
|
|
}
|
|
// y-movement
|
|
|
|
movementDirection = new Vector3(0, deltaY, 0);
|
|
transform.position += movementDirection * verticalSpeed * Time.deltaTime;
|
|
|
|
if (transform.position.y < minYCoordinate)
|
|
{
|
|
transform.position = new Vector3(transform.position.x, minYCoordinate, transform.position.z);
|
|
}
|
|
else if (transform.position.y > maxYCoordinate)
|
|
{
|
|
transform.position = new Vector3(transform.position.x, maxYCoordinate, transform.position.z);
|
|
}
|
|
}
|
|
public void OnTriggerEnter2D(Collider2D other)
|
|
{
|
|
if (other.gameObject.CompareTag("Shop Prefab") && uiElement != null)
|
|
{
|
|
uiElement.SetActive(true);
|
|
}
|
|
|
|
Debug.Log("Aktiviert");
|
|
}
|
|
|
|
public void OnTriggerExit2D(Collider2D other)
|
|
{
|
|
if (other.gameObject.CompareTag("Shop Prefab") && uiElement != null)
|
|
{
|
|
uiElement.SetActive(false);
|
|
}
|
|
|
|
Debug.Log("Deaktiviert");
|
|
}
|
|
|
|
private void ScorePanel()
|
|
{
|
|
scoreText.text = score.ToString();
|
|
|
|
if (score > PlayerPrefs.GetFloat("HighScore", 0f))
|
|
{
|
|
PlayerPrefs.SetFloat("HighScore", score);
|
|
highscoreText.text = score.ToString($"{score}");
|
|
}
|
|
}
|
|
}
|
|
|