Hi,
I have this enemy gameObject called “frog”. I’ve created a script that moves the frog from the start position to the target position. The player has to press the “f” key to make the frog rotate and move back to the start position. If the player collides with the frog, the game will be over (handled by another script).
The current script works fine, but I want to modify it to work as follows:
- The frog should not move instantly when the game starts.
- The frog should spawn repeatedly at random times.
using UnityEngine;
public class FrogScript : MonoBehaviour
{
public GameObject frog; // Frog-peliobjekti
public float moveDuration = 1.2f; // Liikkumisen kesto sekunteina
private Vector3 startPosition; // Alkuperäinen sijainti
private Vector3 targetPosition; // Kohdesijainti
private Vector3 customTargetPosition; // Kohdesijainti, kun “f” näppäintä painetaan
private Quaternion startRotation = Quaternion.Euler(-174.61f, 7.43f, -5.48f); // Alkuperäinen rotaatio
private Quaternion targetRotation = Quaternion.Euler(-174.61f, -180f, -5.48f); // Kohderotaatio
private float rotationDuration = 0.2f; // Rotaation kesto sekunteina
private float elapsedTime = 0.0f; // Kulunut aika liikkumisen aikana
private bool isMoving = false; // Tarkistaa, onko Frog-peliobjekti liikkeessä
public AudioClip screamSound; // “scream.mp3” äänitiedosto
private AudioSource audioSource; // AudioSource-komponentti
private void Start()
{
startPosition = new Vector3(13.6f, 0f, frog.transform.position.z);
targetPosition = new Vector3(2.01f, 0f, -4f);
customTargetPosition = new Vector3(13.6f, 0f, -3.18f);
frog.transform.rotation = startRotation; // Aseta alkuperäinen rotaatio
// Hae AudioSource-komponentti Frog-peliobjektilta
audioSource = frog.GetComponent();
// Aseta “scream.mp3” audioclipiksi
audioSource.clip = screamSound;
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.F))
{
// Jos “frog” ei ole jo liikkeessä, käynnistä liike kohti määriteltyä kohdesijaintia ja rotaation muutos
if (!isMoving)
{
StartCoroutine(MoveFrogSmoothly(customTargetPosition, moveDuration));
StartCoroutine(RotateFrog(targetRotation, rotationDuration));
// Soita “scream.mp3” äänitiedosto
audioSource.Play();
}
}
else
{
// Liikuta Frog-peliobjektia sulavasti alkuperäisestä sijainnista kohdesijaintiin
if (elapsedTime < moveDuration)
{
float t = elapsedTime / moveDuration;
frog.transform.position = Vector3.Lerp(startPosition, targetPosition, t);
elapsedTime += Time.deltaTime;
}
}
}
private System.Collections.IEnumerator MoveFrogSmoothly(Vector3 destination, float duration)
{
isMoving = true;
startPosition = frog.transform.position;
targetPosition = destination;
elapsedTime = 0f;
float startTime = Time.time;
while (elapsedTime < duration)
{
float t = (Time.time - startTime) / duration;
frog.transform.position = Vector3.Lerp(startPosition, targetPosition, t);
elapsedTime += Time.deltaTime;
yield return null;
}
frog.transform.position = targetPosition;
isMoving = false;
}
private System.Collections.IEnumerator RotateFrog(Quaternion targetRotation, float duration)
{
Quaternion startRotation = frog.transform.rotation;
float startTime = Time.time;
float elapsedTime = 0f;
while (elapsedTime < duration)
{
float t = (Time.time - startTime) / duration;
frog.transform.rotation = Quaternion.Lerp(startRotation, targetRotation, t);
elapsedTime += Time.deltaTime;
yield return null;
}
frog.transform.rotation = targetRotation;
}
}