Unity start Audio at specific moment - unity3d

I am new to Unity and I have a little menu which comes up with the logo coming into the screen. Now I want to add a mp3/wav which gives an audio while the logo comes into the screen. The audio should stop when the logo appearance also stops.
How can I put the audio at the exact same moment when the logo (png) appears?
Thank you in advance :)

It might be an overkill solution for your simple case but you could try using Unity Timeline
https://docs.unity3d.com/Packages/com.unity.timeline#1.5/manual/index.html
You can setup all the menu animations with sounds and then play it on click.
https://learn.unity.com/tutorial/starting-timeline-through-a-c-script-2019-3#

All you need to do is have a reference to the AudioSource that has the audio you want to play and use the Play and Stop methods. It is also important to assure you have the field PlayOnAwake turned off as that will play the audio the moment the scene starts.
I do not know how you are currently moving your object, so here is a very generic answer as to how you can approach this using the information I gave above.
[SerializeField] private AudioSource yourAudio = null;
private void IEnumerator Movement
{
yourAudio.Start();
while(movement)
{
// your movement code here (assure there is a yield return null)
}
yourAudio.Stop();
}
With some code or more detail, I can tweak the above snippet but the main idea can be applied to however you are doing your movement. If you want to stop the audio the moment the logo appears on screen, that is a different approach, so let me know if that is what you meant.
Just for completeness, if you want to just stop or start this audio whenever your logo is visible or no longer visible, here is an approach. Be warned though, this approach will be triggered by the editor camera as well, so if something unexpected happens close or toggle off your scene view.
public class YourLogoScript : MonoBehaviour
{
[SerializeField] private AudioSource yourAudio = null;
// stop audio when invisible
void OnBecameInvisible()
{
yourAudio.Stop();
}
// start audio when visible
void OnBecameVisible()
{
yourAudio.Start();
}
}
By using OnBecameInvisible and OnBecameVisible, whenever the renderer attached to your logo is seen by any (including scene view) camera, the OnBecameVisible function is called and when it is no longer seen, the OnBecameInvisible is called.

Related

Unity 2D 2020: How do I get an animation to continue playing on a paused timeline?

The timeline is triggered upon entering the box collider. The timeline plays, and any animations that are slotted in before the signal emitter play, but they paused when the timeline pauses it. They currently play the base animation that they have in place. I need the animation to loop a speech animation instead of pausing it upon reaching the signal emitter that pauses the timeline.
Code on timeline pause is below for anyone that might need it.
`public class PauseTimeline : MonoBehaviour
{
private PlayableDirector director;
void Start()
{
director = GetComponent<PlayableDirector>();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
PlayTime();
}
}
public void PauseTime()
{
director.Pause();
}
public void PlayTime()
{
director.Play();
}
}`
I've tried searching on the Unity website, but they only cover how to stop the animations? How do I keep animations playing upon pausing the timeline? I'm considering trying to have one GameObject with the speech animation and another GameObject with the basic idle animation, and just using a control track to turn them on and off based on what I need. But I don't think this will work?
In the inspector, select the "Update Method" as "Unscaled Game Time" on the Playable Director
Documentation Playable Director

How to make transparent UI visible in Unity Editor?

BackGround
In Unity 2020LTS, I want to make a UI scene.
But in Game Panel, I discovered, although a animation is set at beginning (no conditions), the game will show what I see in Editor panel for a little time at first, then play the animation.
The StateMachine is Entry -> Target(Default)
I don't want show player what I see in editor, but only the first frame in animation.
I guess this is because loading level costs some time (almost 0.5 secs).
Question
So I try another way, make initial state of all objects be same as the first frame of animation.
This way work, seems just like it freeze at first frame for 0.5secs. However, I can't edit those objects visibly (Because they all are transparent in first frame).
I have tried Gizmos, but they don't work well. Besides, Gizmos makes me have to create lots of classes in C# scripts for each object, which just is component of animation and has no script.
Could there be any better way to show transparent (UI) object in editor scene only ?
Assuming you have some Game Manager script, you can add a GameObject, assigning it the UI element, and in the Start() function of the script, make it inactive, like so:
public class GameManager : MonoBehaviour
{
public GameObject menu;
void Start()
{
menu.SetActive(false);
//other statements
}
}
I'm not quite sure what you asked for but if you want to edit the UI elements that are invisible you can simply select them by using the hierarchy and edit them. I have linked an image with an example of this. Example scene with invisible panel
Image img;
// Start is called before the first frame update
void Start()
{
img = GetComponent<Image>();
img.color = new Color32(0, 0, 0, 0);
}
The code above will set the panels transparency to zero when the scene starts, make sure to use the using unity.UI namespace in order to acess the image component.

OnMouseOver works differently on same objects

I am a beginner in Unity and I just found out a behaviour I do not understand...
I have a prefab "cell" that I made from a sprite and I want it to change its color when my mouse is over it.
So I added a BoxCollider2D component to it as well as the following script:
public class Cell : MonoBehaviour
{
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
}
private void OnMouseOver()
{
GetComponent<SpriteRenderer>().color = Color.magenta;
}
private void OnMouseExit()
{
GetComponent<SpriteRenderer>().color = Color.black;
}
}
Then when I drag and drop the "cell" prefab to the scene, it won't work (when my mouse is over the cell, nothing happens).
Same problem when I add another "cell" prefab to the scene.
But when I add a third "cell" prefab to the scene, the feature works on the 2 first cells but not on the third.
I probably missed something or there is a behaviour I do not know, anyway if someone knows why this is happening, please tell me.
Thanks!
I just tested your code in my game using a 2d box sprite and it works fine.
Video > https://youtu.be/6GP3-aV9g3g
You may want to try a couple of things to debug it.
First make sure that there is a BoxCollider2D and Rigidbody2D attached.
Make sure that there is nothing covering the boxes in the scene.
When I am having trouble with an aspect of the game I try to break it down into its simplest components. Try making a scene with nothing in it apart from the box and trying it, if that doesent work, try attaching the scrip to a non prefab object.
Try adding Debug.Log("Mouse Enter"); to the subs to check if the mouse is detected on enter, if it is detecting the mouse, maybe your spite renderer isn't working properly.
Try these things and let me know if they don't work, I would be happy to keep trying to figure it out.

Unity Video Player

I am failing at playing a simple video under unity free 2017.1.0f3 personal
I am working on a game and I'd like to play an introduction video at the start of the app. Then move onto the login screen whenever the player clicks it.
I have created a video player object, dragged and dropped the video clip (mp4) into the video clip field of the object.
I then attached the object to the camera. In the script attached to the camera I created a public VideoPlayer that I have populated with the video player object.
I then execute :
void Awake ()
{
VideoPlayer.Play();
}
But nothing happens.
Perhaps it should be executed within a separate thread (coroutine)? I tried but did not work either.
Any help please?
Provided you filled the right settings, if it still doesn't play but you get no error, no freeze, no nothing.
Trying restarting Unity.
(I struggled for 45 min trying to figure out why my video wouldn't play anymore until I restarted Unity and it magically reworked)
did you check if the Video can be played back by unity?
put a quad in front of your camera, put a videocomponent on there, check Loop and playonAwake, hit Play and see if it works.
GameObjectWithPlayerComponentAttatched.GetComponent<VideoPlayer>().Play();
should work fine
Did you assign the RenderTexture?
What I do is create a RenderTexture, then assign it to the videoplayer, then add a Raw Image and then give it the RenderTexture in the Texture field.
You should uncheck play onAwake if you want to play it at a certain point, instead of videoplayer.Play, use videoPlayer.
Prepare and prepareCompleted Play the video.
Like this:
private void Start()
{
videoPlayer.prepareCompleted += VideoPlayer_prepareCompleted;
videoPlayer.Prepare();
}
private void VideoPlayer_prepareCompleted(VideoPlayer source)
{
videoPlayer.Play();
}
Video Player has three modes: Render Texture -- and then you need to select a RenderTexture to render to, Material Override -- and then it needs a mesh to write to its material, and Camera -- then you need to assign a camera and select to render to the near or far plane.
To have the camera feature work automatically, you need to instantiate the VideoPlayer script on the camera object itself.
Follow these steps to play a video using unity's VideoPlayer component:
Create a plane, under 3d Objects.
Add a VideoPlayer component to that plane.
Set render mode as Material Override.
Drag the mesh renderer component of the plane to the videoplayer's renderer field.
Select a video clip to play and enable play on awake.
Press play in the editor window.

How to change cameras in Unity based on camera coordinates?

I'd like to create a movie in Unity, so I would need several cameras and camerapaths.
On the top of this off course I'd like to change between them. For example if Camerapath1 reaches a significant point with Camera1 then I'd like to change to Camerapath2 with Camera2, etc.
I also have Camera Path Animator asset installed. It's working perfectly when I'm using it with only one camera for several camerapaths but I'm unable to change between maincameras.
I'm a newcomer to Unity. I also know that I should do something like this:
...
camera1.camera.active = false;
camera2.camera.active = true;
...
...but where should I populate these lines? On the top of this, how may I catch the event when a camera on a specific camerapath reaches a particular point?
The way to go would be an animation controller that has all camera as children and controls the active state of all cameras. This provides perfect control over the behaviour.
Create an empty game object, add all cameras as children, add an Animator to the main object with one animation. This animation takes all the camera and set their active state. One extra bonus of this approach is the possibility to call methods as well using the AnimationEvent process. You can still define within the animation for some triggered actions like explosions or movements of objects.
As I said, this give you perfect control since you can define easily actions at specific time.
The downside of it is the rigidity of the process. It may not be as flexible as code, but since you are making a movie, you probably do not need flexibility.
If so, you would have your cameras with a collider and rigidbody (isKinematic true), then you would have some trigger box with a simple script:
public void CameraTrigger:MonoBehaviour{
public GameObject nextCamera;
void OnTriggerEnter(Collider col){
if(col.gameObject.CompareTag("Camera")){
col.gameObject.SetActive(false);
nextCamera.SetActive(true);
}
}
}
Then you drag the camera meant to start next as nextCamera.