Wäre sehr interessant zu sehen mit welchen Wörtern du "gefragt" hast. Das ist ja die Kunst um ChatGPT zu nutzen und was Programmierer vielleicht schnell lernen sollten um nicht zurück gelassen zu werden.
Übung habe ich damit ja auch nicht. Am Beispiel mit dem Spritecontroller bin ich so vorgegangen:
Schreibe ein Script für unity für einen spritecontroller der die animationsphasen für idle, Run und climbing abspielt.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpriteController : MonoBehaviour
{
private Animator animator;
// Animation states
private const string IdleAnimation = "Idle";
private const string RunAnimation = "Run";
private const string ClimbAnimation = "Climb";
void Start()
{
// Holen Sie sich den Animator-Komponenten des GameObjects
animator = GetComponent<Animator>();
}
void Update()
{
// Überprüfen Sie den Eingang des Spielers, um die entsprechenden Animationen abzuspielen
float moveInput = Input.GetAxis("Horizontal");
if (moveInput != 0)
{
// Wenn der Spieler sich horizontal bewegt, spiele die Run-Animation ab
animator.Play(RunAnimation);
}
else if (Input.GetKey(KeyCode.Space))
{
// Wenn der Spieler die Leertaste drückt, spiele die Climb-Animation ab
animator.Play(ClimbAnimation);
}
else
{
// Ansonsten spiele die Idle-Animation ab
animator.Play(IdleAnimation);
}
}
}
Hier ist mir dann aufgefallen, dass nur ein einzelnes Sprite abgespielt wird.
Also Ergänzung.
Jede spriteanimation hat 8 sprites
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpriteController : MonoBehaviour
{
private SpriteRenderer spriteRenderer;
// Animation states
private const string IdleAnimation = "Idle";
private const string RunAnimation = "Run";
private const string ClimbAnimation = "Climb";
// Sprites for each animation
public Sprite[] idleSprites;
public Sprite[] runSprites;
public Sprite[] climbSprites;
// Animation speed
public float animationSpeed = 0.1f;
private void Start()
{
spriteRenderer = GetComponent<SpriteRenderer>();
StartCoroutine(PlayAnimation(IdleAnimation, idleSprites));
}
private IEnumerator PlayAnimation(string animationState, Sprite[] sprites)
{
int spriteIndex = 0;
while (true)
{
// Set the current sprite based on the animation state and index
spriteRenderer.sprite = sprites[spriteIndex];
// Increment the sprite index
spriteIndex = (spriteIndex + 1) % sprites.Length;
yield return new WaitForSeconds(animationSpeed);
}
}
private void Update()
{
float moveInput = Input.GetAxis("Horizontal");
if (moveInput != 0)
{
StopAllCoroutines();
StartCoroutine(PlayAnimation(RunAnimation, runSprites));
}
else if (Input.GetKey(KeyCode.Space))
{
StopAllCoroutines();
StartCoroutine(PlayAnimation(ClimbAnimation, climbSprites));
}
else
{
StopAllCoroutines();
StartCoroutine(PlayAnimation(IdleAnimation, idleSprites));
}
}
Damit kann ich leben.