neuer character modifiziert und skeleton vom alten in den neuen
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
[RequireComponent(typeof(AudioSource))]
|
||||
public class MotionAudioController : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private Transform targetTransform;
|
||||
[SerializeField] private Animator animator;
|
||||
[SerializeField] private AudioSource audioSource;
|
||||
[SerializeField] private Vector3 lastPosition;
|
||||
|
||||
[SerializeField, Tooltip("Threshold for movement detection. Adjust as needed.")]
|
||||
private float movementThreshold = 0.0001f;
|
||||
|
||||
[SerializeField, Tooltip("Duration of the fade-out effect in seconds")]
|
||||
private float fadeOutDuration = 0.5f;
|
||||
|
||||
private bool wasMoving = false;
|
||||
private Coroutine fadeCoroutine;
|
||||
|
||||
void Start()
|
||||
{
|
||||
// Get the AudioSource attached to this GameObject
|
||||
audioSource = GetComponent<AudioSource>();
|
||||
|
||||
// Find the Animator in sibling or child objects
|
||||
animator = GetComponentInParent<Animator>();
|
||||
|
||||
if (animator != null)
|
||||
{
|
||||
targetTransform = animator.transform;
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("No Animator component found in parent or its children.");
|
||||
}
|
||||
|
||||
// Initialize last position
|
||||
if (targetTransform != null)
|
||||
{
|
||||
lastPosition = targetTransform.position;
|
||||
}
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (targetTransform == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if the object has moved significantly
|
||||
float movement = Vector3.Distance(targetTransform.position, lastPosition);
|
||||
bool isMoving = movement > movementThreshold;
|
||||
|
||||
if (isMoving && !wasMoving)
|
||||
{
|
||||
// If there's a fade-out in progress, stop it
|
||||
if (fadeCoroutine != null)
|
||||
{
|
||||
StopCoroutine(fadeCoroutine);
|
||||
fadeCoroutine = null;
|
||||
}
|
||||
|
||||
// Reset volume to full
|
||||
audioSource.volume = 1f;
|
||||
|
||||
// Start playing audio only when movement starts
|
||||
if (!audioSource.isPlaying)
|
||||
{
|
||||
audioSource.Play();
|
||||
}
|
||||
}
|
||||
else if (!isMoving && wasMoving)
|
||||
{
|
||||
// Start fade-out when movement stops
|
||||
if (audioSource.isPlaying)
|
||||
{
|
||||
// If there's already a fade-out in progress, stop it
|
||||
if (fadeCoroutine != null)
|
||||
{
|
||||
StopCoroutine(fadeCoroutine);
|
||||
}
|
||||
fadeCoroutine = StartCoroutine(FadeOut());
|
||||
}
|
||||
}
|
||||
|
||||
// Update movement state
|
||||
wasMoving = isMoving;
|
||||
|
||||
// Update last position for the next frame
|
||||
lastPosition = targetTransform.position;
|
||||
}
|
||||
|
||||
private IEnumerator FadeOut()
|
||||
{
|
||||
float startVolume = audioSource.volume;
|
||||
float currentTime = 0;
|
||||
|
||||
while (currentTime < fadeOutDuration)
|
||||
{
|
||||
currentTime += Time.deltaTime;
|
||||
audioSource.volume = Mathf.Lerp(startVolume, 0, currentTime / fadeOutDuration);
|
||||
yield return null;
|
||||
}
|
||||
|
||||
// Ensure volume is zero and stop the audio
|
||||
audioSource.volume = 0;
|
||||
audioSource.Stop();
|
||||
fadeCoroutine = null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8c2a1f8514c164c1b84d93b38976ba4d
|
||||
@@ -0,0 +1,46 @@
|
||||
using UnityEngine;
|
||||
using TMPro;
|
||||
using System; // Required for Type handling
|
||||
|
||||
public class UpdateCollectibleCount : MonoBehaviour
|
||||
{
|
||||
private TextMeshProUGUI collectibleText; // Reference to the TextMeshProUGUI component
|
||||
|
||||
void Start()
|
||||
{
|
||||
collectibleText = GetComponent<TextMeshProUGUI>();
|
||||
if (collectibleText == null)
|
||||
{
|
||||
Debug.LogError("UpdateCollectibleCount script requires a TextMeshProUGUI component on the same GameObject.");
|
||||
return;
|
||||
}
|
||||
UpdateCollectibleDisplay(); // Initial update on start
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
UpdateCollectibleDisplay();
|
||||
}
|
||||
|
||||
private void UpdateCollectibleDisplay()
|
||||
{
|
||||
int totalCollectibles = 0;
|
||||
|
||||
// Check and count objects of type Collectible
|
||||
Type collectibleType = Type.GetType("Pickup");
|
||||
if (collectibleType != null)
|
||||
{
|
||||
totalCollectibles += UnityEngine.Object.FindObjectsByType(collectibleType, FindObjectsSortMode.None).Length;
|
||||
}
|
||||
|
||||
// Optionally, check and count objects of type Collectible2D as well if needed
|
||||
Type collectible2DType = Type.GetType("Collectible2D");
|
||||
if (collectible2DType != null)
|
||||
{
|
||||
totalCollectibles += UnityEngine.Object.FindObjectsByType(collectible2DType, FindObjectsSortMode.None).Length;
|
||||
}
|
||||
|
||||
// Update the collectible count display
|
||||
collectibleText.text = $"Collectibles remaining: {totalCollectibles}";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 12b990e8d7f314383856263e97f1c328
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user