What i want is when running the game that the animation clip will start playing automatically.
I added to the Main Camera a Animation component.
Added in the Inspector to the Animation the clip file name Camera_Sign003 also set the Animations size to 1 and added there the same clip Camera_Sign003.
Also i set to true the Play Automatically.
I also attached a script to the Main Camera:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraPoints : MonoBehaviour
{
public Animation anim;
IEnumerator Start()
{
anim = GetComponent<Animation>();
anim.Play(anim.clip.name);
yield return new WaitForSeconds(anim.clip.length);
}
}
And i checked with a break point it's getting to the line anim.Play....And the length is 10 but nothing happen after 10 seconds.
The animation is a short clip moving the camera from point to point.
The Main Camera is child under ThirdPersonController.
The Animation component is a legacy component and shouldn't be used unless you need to support a legacy tool or project.
What you really need is an Animator component with an Animation Controller for the object. The easiest way to set this up is by selecting the object you want to animate (e.g. Main Camera), opening the 'Animation' window, and clicking the 'Create' button there.
The new animation created will play automatically and loop by default.
More information can be found here: Unity - Manual: Creating a New Animation Clip
Related
I'm using Unity 2020.3.21f1, Universal Render Pipeline
Hi, I'm new to this, I'm adding a second camera to my scene through using 'Don't Destroy on load'
The Previous scene (Scene1) Has this:
void Start()
{
SceneManager.LoadSceneAsync(LoadingData.sceneToLoad);
//Static script called
}
And the menu and camera items in this scene have this attached:
void Start()
{
DontDestroyOnLoad(gameObject);
}
So when Scene2 loads, there will be a main camera, and Scene1's Camera. However, in the game mode, when I rollover the (Scene2) main camera's stack, it says it cannot detect an overlay camera, even though the Scene1's overlay camera is in the Scene2 Hierarchy?
When I manually copy over the Scene1 Overlay camera to Scene2, the stack can detect it, and everything works perfect. It is only when using don't destroy on load that it cannot detect a camera?
This might be unrelated, but I am trying to use:
public void CheckForCamera()
{
var cameraData = GetComponent<Camera>().GetUniversalAdditionalCameraData();
cameraData.cameraStack.Add(OverlayCamera);
}
To add the camera, but even with this, when I roll over the main camera stack, it says overlay camera cannot be detected, so I'm guessing I need to figure out why it can't be detected first before this script can be tested.
I have also made sure their tags are different. Scene1 Camera has the tag 'HUDCamera' and Scene2 Main Camera has the tag main camera
Scene1 Camera (HUD Menu Script contains just don't destroy on load):
Scene 1 Camera Inspector Details
Scene2 Camera (Camera Controller Script follows player around)
Scene2 Camera Inspector Details
I've been stuck on this for ages, someone please help!
This is the current script that I have. I have a ball that is player, and I want a collision with my game object tagged as "pit" to end the game. When the game ends I want my game over canvas to pop up. The current script detects the hit when the player rolls over the pit, however, currently, nothing else happens. Both my player and pit have rigidbodies and colliders attached to them. I would really appreciate any help with how to get my code to work properly.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Trigger : MonoBehaviour
{
public GameObject GameManager;
private void OnTriggerEnter(Collider other)
{
Debug.Log("hit detected")
if(other.gameObject.tag == "pit")
{
GameManager.GetComponent<Game_Manager>().EndGame();
}
}
}
My EndGame code is in my GameManager script. The is the part of that script that deals with ending the game.
public void EndGame ()
{
player.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezePosition;
hasGameStarted = true;
inMenuUI.gameObject.SetActive(false);
inGameUI.gameObject.SetActive(false);
gameOverUI.gameObject.SetActive(true);
}
If I'm correct in interpreting your question, your script successfully detects player death already. If you want a game over screen you have a couple of options:
You can create a UI canvas with a panel on it that obscures the whole screen and put the words Game Over.
Or, probably the better option is to create an entirely new Unity scene and make the Game Over canvas there. When you want to trigger the game over screen you simply use:
Scene manager.LoadScene("Scene name");
You have to add "using UnityEngine.SceneManagement;" at the top of your script. Also note you have to add your Game Over scene to the list of scenes in you game. I believe this can be done by navigating to File>Build settings>scenes and then pressing add open scenes assuming the game over scene is open.
I am trying to start an animation by pressing a key on my keyboard.
For this I am using the following code:
[HideInInspector] public Animation animation;
private void Start()
{
animation = GetComponent<Animation>();
}
private void Update()
{
if (Input.GetKeyDown("1"))
{
animation.Play("AnimationName");
}
}
This gives me the following error:
The animation state AnimationName could not be played because it
couldn't be found!
Although, the animation DOES start, but how can I clear this error?
I think the only possible explaination is that your animation clip is not marked as Legacy.
https://www.unity3dtips.com/zh/the-animation-state-could-not-be-played-because-it-couldnt-be-found/
EDIT with step by step here:
Animations must be marked as legacy to be used with the animation component.
In the project window select the animation clips you’re trying to play.
Set the inspector to debug mode which exposes hidden variables, in this case it’ll make the “Legacy” checkbox appear.Unity Inspector Debug Mode
Tick the “Legacy” checkbox and change the inspector back to normal mode.
Unity Inspector Animation Clip Legacy Checkbox
But there is one thing you should know: you should avoid, if possible, the old legacy way to play animation.
Learn how to use an Animator component. Just create an animator, assign your animation clip, decide a boolean for start the animation in the animator component and set it to true in your code (for basic use).
Sure there is some things to do beside code but it will be easier to control your animation, bleand different animations and change them to your needs (it's for example much easier to loop or stop the animation at some point).
It would be also easier to understand when it start and when is playing.
Another thing to check is the legacy 'Animation' component on your gameObject that is supposed to perform the animation. In my case things were marked as legacy for the animation on the imported animation but somehow when adjusting settings on my import it dropped the animation it was complaining about from the actual 'Animation' component so once I added it things worked as expected
Inspector in Debug mode showed my animation marked as 'Legacy' true
My Animation component showed the missing animation
In Unity 3D (2D mode), I have a few Game Objects linked with Animator, and animation made with Animation Timeline. The animation is initially disabled:
public GameObject car;
Animator carAnim;
void Start() {
carAnim = car.GetComponent<Animator>();
carAnim.enabled = false;
}
Then, with an input event, the animation is set to enable:
void Update() {
if(Input.GetKeyDown(0)) {
carAnim.enabled = true;
}
}
However, when the animation starts to play, the whole screen flickers once. How to remove the flicker?
I think the problem is that you're trying to enable and disable Animator component at runtime. Animator was not designed to be used like this. If you want to play animation in certain point of time, just keep Animator enabled, create 2 states - "Idle" and "YourAnimation", create transition between them and control it with some bool parameter.
Much simple way is to use Animation component (if you don't need Mecanim and transitions between states), where you can play animation with just one line of code:
gameObject.GetComponent<Animation>().Play("MyAnimation");
Tried to find this kind of problem around the net but failed so...
Here's the thing - I have a prefab gameobject that is to represent a unit, portrait more specifically. It has several scripts attached and an Animation component with two animations: Static and Selected.
The prefab is instantiated, moved freely and, after placing, it can be clicked to select it, which should, aside from executing a bit of code, start the Selected animation.
Using this code:
void OnMouseDown(){
//
//Some inside stuff
//
if (this.GetComponent<UnitHandling> ().thisUnit.Selected)
this.animation.Play("Selected");
if(this.animation.IsPlaying ("Selected"))
Debug.Log("Animation of selection is playing");
}
I checked that the animation seems to be playing (at least the Debug message is showing) but I see no animation... What am I doing wrong?
Try making an animation state using mechanim, and play it using this:
GetComponent<Animator>().CrossFade("Selected", 0);
https://docs.unity3d.com/Documentation/ScriptReference/Animator.CrossFade.html
https://docs.unity3d.com/Documentation/Manual/MecanimAnimationSystem.html