Basically, In my scene, underneath a GameObject called 'GameOverUI' I have some buttons and a Panel that covers the screen. I have made a simple animation where the panel's opacity (The alpha channel) increases. Do any of you know how to make it so that when the player dies, it enables GameOverUI and plays the animation for the panel once?
Edit: Forgot to mention, I know how to make it so that 'GameOverUI' is enabled, I just don't know how to make the animation play
If you are looking for a solution with animations, then here you go:
Create a script for your GameOverUI gameObject. Really simple:
public class UIHandler : MonoBehaviour
{
private void OnEnable()
{
//play "dead screen" animation
}
}
Method OnEnable() is a MonoBehavior function, and will be called when you enable a GameObject by calling myGameObject.SetActive(true);.
However, I would recommend thinking about another solution. I usually leave GameOverUI always active and use it to manage its children via script. So I think it would be more elegant to write something like this:
public class UIHandler : MonoBehaviour
{
public void PlayerDied()
{
//play "dead screen" animation
}
}
The difference is, that instead of enabling and disabling the GameObject, you call a method. This way, you will be able to pass data (as function parameters) if you need to do so. For example, you can write to the screen, what caused the death. And further on, the GameObject will be able to manage its UI components for other purposes.
I hope it makes sense and I could be of your help!
Related
I'm trying to make a game where you're a boy and you can transform into a deer.
I don't know how to make the player transform into a deer when pressing a button. Can someone please tell me how to make the player transform into a deer when pressing a button and how to make the deer transform back into the player when pressing the button again?
There are 2 ways depending on what you want exactly.
1.Transformation without an animation, you can do this by having 2 separate models
public GameObject boy;
public GameObject deer;
//Give both objects the same tag and don't add anything else under the
tag in order for this to work
private Transform currentPosition;
private void Start()
{
deer.SetActive(false);
}
public void TransformButton()
{
currentPosition = GameObject.FindObjectWithTag(LayerMask.NameToLayer("TransformationTag"));
//Unity doesn't pick up disabled GameObjects so it will pick up only the
//active state (the active object)
boy.SetActive(!activeInHierarchy);
deer.SetActive(!activeInHierarchy);
GameObject.FindObjectWithTag(LayerMask.NameToLayer("TransformationTag")).getComponent<Transform>() = currentPosition;
}
I recommend this way if your deer and boy have different scripts
you could also play an animation on click (apply this animation to the active character) and when it is done (below is how to check if it is done) you can trigger the code above
https://answers.unity.com/questions/362629/how-can-i-check-if-an-animation-is-being-played-or.html
I am pretty sure there are other better ways but this is the way I do it
The other way which is probably easier if you are good at animating is to just trigger an animation using the animation bools when the button is clicked but I haven't tried this way because I'm not that good at animation. Instead I use the first way (usually for switching skins in a shop for example) but I never did animations that completely change the size ratio so much so I don't know how well would it work (I usually do 1:1 transformations like humanoid to humanoid or tank to tank so you will have to experiment a bit)
I dont think that this is possible in unity. Maybe there are some Assets that offer something like this.
But you could create a similar effect with Blender.
Check out Morphing Shape Animations.
There are some cool tutorials out there Morphing Shape Animations in Blender
So I have an animation that fades into the menu screen but after the animation ends none of my buttons work. I have figured out that it is because the GameObject that holds the black image that fades to clear is always in the front, blocking me from using any of the buttons. I tried to write a script, that's attached to the game object, that disables the GameObject after it completes the animation, but it isn't working.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LevelChanger : MonoBehaviour
{
public Animation anim;
public void SetTrigger()
{
this.StartCoroutine(this.PerformAnimRoutine());
}
private IEnumerator PerformAnimRoutine()
{
var state = anim.PlayQueued("Fade_In", QueueMode.PlayNow, PlayMode.StopSameLayer);
yield return new WaitForSeconds(state.length);
this.gameObject.SetActive(false);
}
}
Is there anything wrong with the code or is there an easier way to accomplish this? I am extremely new to unity so I am very stuck.
If all you are doing is fading a sprite to clear and you seem to know about coroutines, I might start by suggesting you do the fade within a coroutine instead.
Have that decrease the alpha by some fraction each frame and when it's 0 disable the object.
That's just if that sounds more fluid, nothing wrong with the animation way.
Doing it with animations though :
I'm not confident that you can disable the object the animation is on in that animation. If it is available on the dope sheet try that. Otherwise we can use state behaviours or animation events.
Animation Events
These can be used to trigger a function at a certain point of an animation. You can create them similar to keyframes. Here is a link to Unity's guide on this topic.
All you'd need to do is create an event and place it at the end of the animation. Then you need to in a script of that object make a public function that simply disables the object. Call that with the event.
State Behaviours
State Machine Behaviours allow you to define a script to run on a given animation state. It has many functions to hook onto such as OnStateEnter and OnStateExit.
You'd want to click on the state that fades in the animator. In the inspector you should be able to click "Add Behaviour". This will create a script that you can open and edit. Here is the reference for that class.
From there is should be very simple to disable the object through OnStateExit.
I made an animation using Animation panel, which swaps images from time to time. Read from the Internet, this is not a legacy animation.
Here is the Animation panel screenshot:
Then, I add the Animation and Animator components to the Game Object and assign the animation, which is called Animation01, to it. Here is the screenshot from Inspector of the Game Object:
I try to use the following C# script to stop the animation :
using UnityEngine;
using System.Collections;
public class Scene1 : MonoBehaviour {
public GameObject ball;
// Use this for initialization
void Start () {
ball.animation.Stop();
}
// Update is called once per frame
void Update () {
}
}
but the animation didn't stop. It prompts a notice in Console:
Default clip could not be found in attached animations list.
What did I miss?
UPDATE: By disabling/ removing Animator component, the animation is stopped and cannot be controlled by codes. I need to control the animation by codes.
Unity had implemented two animation systems throughout it's history. Animation component belongs to the legacy animation system, while Animator component belongs to the new animation system. So, adding both components makes no sense: you either use the old system or the new.
The main difference between the legacy and new animation system is that the new animation system is much more advanced in way it's controlled. However, it also means that it's more complicated. It adds another level of abstraction: instead of launching animation yourself, you control variables that influence the behaviour of a special state machine, animation controller.
So, if you want to use animations for something really, really simple, where you want just to launch animations manually, it may be better to use legacy animation system instead. But the components are not the only thing that is different: the animation files themselves are marked to determine if they are "legacy" or not. By default, when you create an animation in the new unity version, it belongs to the new animation system. If you want to use it with a legacy animation, you have to mark it as a legacy animation. Unfortunately, you'll have to do a little hack to achieve that.
is there anyway to draw stuff on scene view without calling OnSceneGUI?
or is there a way to get OnSceneGUI to get called even if the object with the script attached is not selected?
Edit: Looks like I wasn't explicit enough on what I was asking for so here's a little update:
I have a whole bunch of controls shown as GUI objects on my Game Scene that are used by the game designers to more easily test the game. Since these tools are for development use rather than deployment, I wanted to move these to the scene window instead.
Unfortunately I am only able to display these GUI tools if the object that contains the script with the "OnSceneGUI" method is selected. Instead I want them to be displayed whenever the game is running and regardless of the selected object.
I have found the solution to my issue.
If you want your GUI stuff to always be displayed on the scene window you can do the following:
public class MyClass{
static MyStaticConstructor() {
SceneView.onSceneGUIDelegate += OnScene;
}
static void OnScene(SceneView sceneView) {
// Draw GUI stuff here for Scene window display
}
}
This code will run as soon as you press play and will draw whatever you wish on your scene window.
Hope it will help others!
There are many ways to use GUI,
The easiest but least efficient way is using the GUI class on OnGUI()
GUI.Label(new Rect(10,10,10,10), "This is a label");
GUI.Label(new Rect(10,10,10,10), "Your Texture2D here");
Any active monobehaviour will run OnGUI() if its defined. So it can be attached to any gameObject. You can create an empty gameObject in the scene and call it "GuiGameObject" for example and attach the script there. That way it wont be mixed in with your gameplay script.
There are also GUI textures --> More Info on GUITexture
Also I recommend checking out nGUI
Edit:
For OnSceneGUI You can try Editor.Repaint, you can use it to make sure that the inspector updates changes made inside of OnSceneGUI
I'm using the old animation system in Unity to keyframe in a camera path (no choice in this). What I'd like to know is if it's possible to have this animation start a script that starts an object to grow in scale depending on the distance of the camera.
I know there is a few ways I could go about this, but I'm specifically asking if it can be done via the animation component. If it is possible, please let me know.
Sure, something like that is easily possible with an Animation Event.
In your animation window, click on the appropriate section in your timeline to add an animation event.
To your object, add a script component that contains the appropriate function you want to call from that animation event. Let's say we create a simple "Test" script that has a public void DoSomething().
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour
{
void Start() {}
void Update() {}
public void DoSomething()
{
Debug.Log ("Do Something");
}
}
Then click on the animation event and a windows like the following should appear:
From the dropdown you should be able to select your method. Now, if you play your animation, then at the moment of your animation event, the appropriate method will be called.
That should do the trick.