Skip to content

Instantly share code, notes, and snippets.

@Keldrik
Last active March 9, 2023 22:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Keldrik/9e086db79a25b1acd55f to your computer and use it in GitHub Desktop.
Save Keldrik/9e086db79a25b1acd55f to your computer and use it in GitHub Desktop.
Unity Einfache Bewegung Beispiel Script
using UnityEngine;
using System.Collections;
public class Bewegen : MonoBehaviour
{
// public member eines Scripts können bequem
// im Unity Editor gesetzt und auch während
// das Spiel getestet wird verändert werden.
public float Geschwindigkeit;
public float Beschleunigung;
float aktuelleGeschwindigkeit = 0;
Vector3 richtung = Vector3.zero;
void Start()
{
}
void Update()
{
if (EingabeÜberprüfen())
{
if (aktuelleGeschwindigkeit < Geschwindigkeit)
{
aktuelleGeschwindigkeit += Beschleunigung * Time.deltaTime;
}
this.transform.position += richtung * aktuelleGeschwindigkeit * Time.deltaTime;
}
else
{
aktuelleGeschwindigkeit = 0f;
}
}
private bool EingabeÜberprüfen()
{
bool eingabe = false;
float x = 0f;
float z = 0f;
// Input.GetKey gibt true zurück wenn
// die gefragte Taste gedrückt ist
if (Input.GetKey(KeyCode.RightArrow))
{
x++;
eingabe = true;
}
if (Input.GetKey(KeyCode.LeftArrow))
{
x--;
eingabe = true;
}
if (Input.GetKey(KeyCode.UpArrow))
{
z++;
eingabe = true;
}
if (Input.GetKey(KeyCode.DownArrow))
{
z--;
eingabe = true;
}
richtung = new Vector3(x, 0f, z);
return eingabe;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment