in my project I want a sound to play and then for an objects active state to be set to false, at the moment both are happening at the same time so the sound doesn't play. If I keep the active state as true then I will hear the sound play.
How can I make sure that the audio finishes before the set active state is switched to false, any help or advice is appreciated.
Here's a selection of my code;
void OnTriggerEnter(Collider other)
{
if (other.tag == "Pick Up")
{
if (!isPlayed) {
source.Play ();
isPlayed = true;
}
}
if (other.gameObject.CompareTag ("Pick Up"))
{
other.gameObject.SetActive (true);
count = count + 1;
SetCountText ();
}
}
You could hide the object using its renderer, as well as turning off any colliders, play the sound, then destroy/set it to inactive:
renderer.enabled = false;
gameObject.collider.enabled = false;
audio.PlayOneShot(someAudioClip);
Destroy(gameObject, someAudioClip.length); //wait until the audio has finished playing before destroying the gameobject
// Or set it to inactive
Use coroutine as Joe Said. Start coroutine each time the collided object is enabled.
void OnTriggerEnter(Collider other)
{
if (other.tag == "Pick Up")
{
if (!isPlayed) {
source.Play ();
isPlayed = true;
}
}
if (other.gameObject.CompareTag ("Pick Up"))
{
other.gameObject.SetActive (true);
count = count + 1;
SetCountText ();
StartCoroutine(waitForSound(other)); //Start Coroutine
}
}
IEnumerator waitForSound(Collider other)
{
//Wait Until Sound has finished playing
while (source.isPlaying)
{
yield return null;
}
//Auidio has finished playing, disable GameObject
other.gameObject.SetActive(false);
}
Related
Okay, this is probably a dumb question but I'm new to this. I have an enemy AI that walks toward the player only when the enemy is visible to the player and the space key is pressed. I want to make a second if statement that makes the enemy run if the player presses the space bar a second time while the enemy is walking or if the enemy is within 2 meters of the of the players current position.
{
public NavMeshAgent enemy;
public Transform player;
public float speedWalk = 6f;
public float speedRun = 60f;
public float groundDrag;
public float playerHeight;
bool isWalking;
Renderer m_Renderer;
void Move(float speed)
{
enemy.speed = speed;
}
private void Start()
{
m_Renderer = GetComponent<Renderer>();
isWalking = false;
enemy.speed = speedWalk;
}
private void OnBecomeInvisible()
{
enabled = false;
}
//DelayEnemyChase
IEnumerator delayChase()
{
yield return new WaitForSeconds(2);
Move(speedWalk);
enemy.SetDestination(player.position);
}
//Visible by camera
void OnBecameVisible()
{
enabled = true;
//starts walking towards player position
if ((Input.GetKey(KeyCode.Space)) && (m_Renderer.isVisible) && (isWalking == false))
{
StartCoroutine(delayChase());
isWalking = true;
}
//starts walking towards player position
else if ((isWalking == true) && (Input.GetKey(KeyCode.Space)) && (m_Renderer.isVisible))
{
Move(speedRun);
enemy.SetDestination(player.position);
isWalking = false;
}
}
private void Update()
{
//sees if enemy is visible + space bar is pressed
OnBecameVisible();
}
}
This is confusing the heck out of me, this is what I have and it's not workign at all. Any help is appreciated!!!!
The main issue is that GetKey is fired every frame as long as the button stays pressed!
You rather want to use GetKeyDown in order to track only the first key press.
Then you currently also start and run multiple concurrent Coroutines!
I would rather use a kind of state routine and do e.g.
private void OnBecomeInvisible()
{
StopAllCoroutines();
enemy.enabled = false;
}
private void OnBecameVisible()
{
enemy.enabled = true;
Move(0f);
enemy.SetDestination(enemy.transform.position);
StartCoroutine (StatesRoutine());
}
private IEnumerator StatesRoutine ()
{
// wait until the space is pressed the first time
// here it depends on what exactly you want to do
// you can either already track if the key is still pressed already
yield return new WaitUntil (() => Input.GetKey(KeyCode.Space));
// or rather wait until the key goes down the first time after having become visible
//yield return new WaitUntil (() => Input.GetKeyDown(KeyCode.Space));
yield return new WaitForSeconds(2);
Move(speedWalk);
enemy.SetDestination(player.position);
// Then for the second press we definitely wait until it gets down again instead of
// only checking if the button is still pressed
// except again your use case actually wants that behavior
yield return new WaitUntil (() => Input.GetKeyDown(KeyCode.Space));
Move(speedRun);
enemy.SetDestination(player.position);
}
Some things still depend on your exact needs though, in particular what shall happen if the enemy becomes invisible. For now I assume you wanted to reset the behavior and start the process of handling space clicks from scratch.
This question already has answers here:
How to make the script wait/sleep in a simple way in unity
(7 answers)
Closed last year.
So I am currently trying to make a game and I want one of the features to be when you hit a certain block I called it Obstacle you will pause for 1 second. I just don't know how to add that pause in C#.
The script:
using UnityEngine;
public class PlayerCollision : MonoBehaviour {
public Movememt movement;
void OnCollisionEnter (Collision Collisioninfo)
{
if (Collisioninfo.collider.name == "Obstacle")
{
movement.enabled = false;
// i want the pause here
movement.enabled = true;
}
}
}
You can do it using coroutines.
You can change the return value from void to IEnumerator which will allow you to "pause" by yeilding a new WaitForSeconds instance. Here is an example:
IEnumerator OnCollisionEnter(Collision collision)
{
if (collision.collider.name == "Obstacle")
{
movement.enabled = false;
yield return new WaitForSeconds(1);
movement.enabled = true;
}
}
You can use WaitForSeconds with coroutines. For details please go through the link.
yield return new WaitForSeconds(1);
You can pause or resume the game by setting the timescale to 0 or back to 1.
void PauseGame()
{
Time.timeScale = 0;
}
void ResumeGame()
{
Time.timeScale = 1;
}
I'm working on a game and I have a my player with the tag "Player". I have a cyclist spawning system, and each cyclist has the tag "cyclist", they are all prefabs. I have a script on the cyclists prefab that makes it move forwards, so each time it spawns, it instantly begins to move in a fixed direction.
I want the cyclists to be able to detect 2 things, one being if the player is in front of it, and the other if another cyclist is in-front of it. If so, I want the cyclist to stop cyclist. I have a script called cyclistStoping.cs that I'm using to do this. The script has been placed onto the cyclist prefab for multiple instances to be spawned.
Bug
I've noticed that sometimes even once a cyclist(a) is no longer in front of another cyclist(b), cyclist(b) will still remain still, and it will only move off again if the player walks in front of it and then walks off again. I feel like my code is just buggy and would really appreciate some help on this.
I've tried to do 2 simple checks inside each trigger function but some cyclists still remain idle when once the cyclist is no longer in front of them.
I have also tried using a delay function so whenever the cyclist or player are no longer in range, the cyclist doesn't move off until 2-3 seconds have passed. However with this, the cyclist doesn't move off again.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class cyclistStoping : MonoBehaviour
{
public VehicleMove cyclistMovement;
public bool playerInside = false;
public bool cyclistInside = false;
private bool timePassed = false;
void Start()
{
}
void Update()
{
}
// implement delay
// player or cyclist (INSIDE)
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player" ) {
playerInside = true;
cyclistInside = false;
}
else if (other.gameObject.tag == "Cyclist")
{
playerInside = false;
cyclistInside = true;
}
if (playerInside == true || cyclistInside == true) {
Debug.Log("Player inside: " + playerInside);
cyclistMovement.vehicleMovement = 0.0f;
}
}
// player or cyclist (EXIT)
private void OnTriggerExit(Collider other)
{
if (other.gameObject.tag == "Player")
{
playerInside = false;
cyclistInside = false;
}
else if (other.gameObject.tag == "Cyclist")
{
playerInside = false;
cyclistInside = false;
}
if (playerInside == false || cyclistInside == false)
{
if (timePassed == true) {
Debug.Log("Player inside: " + playerInside);
cyclistMovement.vehicleMovement = 0.1f;
// delay, then move off
timePassed = false;
}
}
}
IEnumerator timeDelay()
{
// wait before moving off
yield return new WaitForSeconds(3);
timePassed = true;
}
}
I expect the cyclist to stop if a player is in front of it, and once the next cyclist spawn, I expect THAT cyclist to stop once it detects the first cyclist. Once the player moves away from the first cyclist, it should take 2 seconds before moving off, and the second cyclist will do exactly the same.
The main problem in your code is that the logic to make the bike move again should be in the IEnumerator, not the OnTriggerExit. Start the Coroutine in OnTriggerExit:
private void OnTriggerExit(Collider other)
{
if (other.gameObject.tag == "Player")
{
playerInside = false;
}
else if (other.gameObject.tag == "Cyclist")
{
cyclistInside = false;
}
if (playerInside == false && cyclistInside == false)
{
StartCoroutine(timeDelay());
}
}
And check again in the IEnumerator:
IEnumerator timeDelay()
{
// wait before moving off
yield return new WaitForSeconds(3);
if (playerInside == false && cyclistInside == false) {
cyclistMovement.vehicleMovement = 0.1f;
}
}
In my script i did that when the player is on the top of the platform move it up.
It's working fine. But now i want to make that once it got up play the clip "Down".
using UnityEngine;
using System.Collections;
using System.Reflection;
public class DetectPlayer : MonoBehaviour {
GameObject target;
public void ClearLog()
{
var assembly = Assembly.GetAssembly(typeof(UnityEditor.ActiveEditorTracker));
var type = assembly.GetType("UnityEditorInternal.LogEntries");
var method = type.GetMethod("Clear");
method.Invoke(new object(), null);
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.name == "Platform")
{
Debug.Log("Touching Platform");
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.name == "OnTop Detector")
{
Debug.Log("On Top of Platform");
GameObject findGo = GameObject.Find("ThirdPersonController");
GameObject findGo1 = GameObject.Find("Elevator");
findGo.transform.parent = findGo1.transform;
target = GameObject.Find("Elevator");
target.GetComponent<Animation>().Play("Up");
}
}
}
After the line
target.GetComponent<Animation>().Play("Up");
I want when it finish playing it play the down:
target.GetComponent<Animation>().Play("Down");
While both answers should work, another method of doing this with coroutine and the IsPlaying function. You use the coroutine solution if you also want to perform other task after the animation.
For the Animation system:
The old Unity animation playback system. This should not be used in your new Project unless you are still using old Unity version.
IEnumerator playAndWaitForAnim(GameObject target, string clipName)
{
Animation anim = target.GetComponent<Animation>();
anim.Play(clipName);
//Wait until Animation is done Playing
while (anim.IsPlaying(clipName))
{
yield return null;
}
//Done playing. Do something below!
Debug.Log("Done Playing");
}
For the Animator system
This is the new Unity animation playback system. This should be used in your new Project instead of the Animation API. In terms of performance, it's better to use the Animator.StringToHash and compare the current state by hash number instead of the IsName function which compares string since the hash is faster.
Let's say that you have state names called Jump, Move and Look. You get their hashes as below then use the function for playing and waiting for them them below:
const string animBaseLayer = "Base Layer";
int jumpAnimHash = Animator.StringToHash(animBaseLayer + ".Jump");
int moveAnimHash = Animator.StringToHash(animBaseLayer + ".Move");
int lookAnimHash = Animator.StringToHash(animBaseLayer + ".Look");
public IEnumerator PlayAndWaitForAnim(Animator targetAnim, string stateName)
{
//Get hash of animation
int animHash = 0;
if (stateName == "Jump")
animHash = jumpAnimHash;
else if (stateName == "Move")
animHash = moveAnimHash;
else if (stateName == "Look")
animHash = lookAnimHash;
//targetAnim.Play(stateName);
targetAnim.CrossFadeInFixedTime(stateName, 0.6f);
//Wait until we enter the current state
while (targetAnim.GetCurrentAnimatorStateInfo(0).fullPathHash != animHash)
{
yield return null;
}
float counter = 0;
float waitTime = targetAnim.GetCurrentAnimatorStateInfo(0).length;
//Now, Wait until the current state is done playing
while (counter < (waitTime))
{
counter += Time.deltaTime;
yield return null;
}
//Done playing. Do something below!
Debug.Log("Done Playing");
}
For a solution specifically for your particular problem with the collision callback function (OnTriggerEnter), there are two possible ways to do that:
1.Start a coroutine function to play the animation after trigger detection:
void OnTriggerEnter(Collider other)
{
if (other.gameObject.name == "OnTop Detector")
{
Debug.Log("On Top of Platform");
GameObject findGo = GameObject.Find("ThirdPersonController");
GameObject findGo1 = GameObject.Find("Elevator");
findGo.transform.parent = findGo1.transform;
target = GameObject.Find("Elevator");
StartCoroutine(playAnim(target));
}
}
IEnumerator playAnim(GameObject target)
{
Animation anim = target.GetComponent<Animation>();
anim.Play("Up");
//Wait until Up is done Playing the play down
while (anim.IsPlaying("Up"))
{
yield return null;
}
//Now Play Down
anim.Play("Down");
}
OR
2.Make the OnTriggerEnter function a coroutine(IEnumerator) instead of void function:
IEnumerator OnTriggerEnter(Collider other)
{
if (other.gameObject.name == "OnTop Detector")
{
Debug.Log("On Top of Platform");
GameObject findGo = GameObject.Find("ThirdPersonController");
GameObject findGo1 = GameObject.Find("Elevator");
findGo.transform.parent = findGo1.transform;
target = GameObject.Find("Elevator");
Animation anim = target.GetComponent<Animation>();
anim.Play("Up");
//Wait until Up is done Playing the play down
while (anim.IsPlaying("Up"))
{
yield return null;
}
//Now Play Down
anim.Play("Down");
}
}
One way to do this without needing to check manually at all is to use queuing
target.GetComponent<Animation>().PlayQueued("Down", QueueMode.CompleteOthers);
This code will wait for any other animation currently playing on the object to finish before playing the queued animation.
The Unity API page regarding this topic
in my project im trying to count the diferent objects and simulate a little animation, for example i have stars in my game, and i want to count the number of stars in the final of the game from 0 trough the number of stars the user got, so i did this:
public void youWin()
{
audio.Stop ();
StartCoroutine (activatePanel ());
}
IEnumerator activatePanel()
{
yield return new WaitForSeconds (3f);
pausePanel2.SetActive (true);
for (int i = 0; i <= stars; i++) {
yield return new WaitForSeconds (0.2f);
starText2.text = i + "";
}
}
my code worked well for 0.3f on the for loop wait for seconds, but it is too slow, i want it for 0.2f, but something strange happen sometimes it get like a bug and the first number seems to go back, it doesn't count right, someone know what is happening?
It very likely that the activatePanel function is being called from another place while it is already running or the script that contains this code is attached to multiple GameObjects and the activatePanel is again, being called by another function. You can use flag to stop this from happening.
If the coroutine function is already running, use yield break; to break out of it.
bool isRunning = false;
IEnumerator activatePanel()
{
//Exit if already running
if (isRunning)
{
yield break;
}
//Not running, now set isRunning to true then run
isRunning = true;
yield return new WaitForSeconds(3f);
pausePanel2.SetActive(true);
WaitForSeconds waitTime = new WaitForSeconds(0.2f);
for (int i = 0; i <= stars; i++)
{
yield return waitTime;
starText2.text = i.ToString();
}
//Done running, set isRunning to false
isRunning = false;
}
Well i solved it with all of you guys help, actually you all where right, i thaught i was calling the youWin function just 1 time, but i forgot this is unity and i called the youWin inside a trigerEnter function, that means that the object keep enter the triger function and called the youWin function, thank you all here is what i mean with that
Solved it with the bool entered
public class Victory : MonoBehaviour {
Manager gameManager;
// Use this for initialization
public AudioClip clip;
private AudioSource audio;
public Animator girl;
private bool entered;
void OnTriggerEnter(Collider c)
{
if (c.gameObject.tag == "Player" && !entered) {
gameManager.win = true;
audio.clip = clip;
audio.Play ();
gameManager.Ball.GetComponent<MoveBall> ().enabled = true;
girl.SetBool ("win",true);
entered = true;
gameManager.youWin ();
}
}
void Start () {
gameManager = GameObject.Find ("GameController").GetComponent<Manager> ();
audio = GetComponent<AudioSource> ();
entered = false;
}
// Update is called once per frame
void Update () {
}
}