Hi guys I'm pretty new to unity and I am trying to make an animation where some of the meshes within a prefab only appear ~halfway through the animation.
I have added an event at the desired time within animator but I cant figure out exactly what code I need to execute to turn the mesh renderer on.
So far I have this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class toggleMesh : MonoBehaviour
{
public Renderer rend;
// Start is called before the first frame update
void Start()
{
rend = GetComponent<Renderer>();
rend.enabled = false;
}
// Update is called once per frame
public void toggleMeshR()
{
rend.enabled = true;
}
}
Any help with this would be appreciated
If you have an animation as you said, put an animation event on the frame(time) you desire to call your toggleMeshR().
When you right click on timeline of animation, you can create animation events.
More information: https://docs.unity3d.com/Manual/script-AnimationWindowEvent.html
Related
I'm new to Unity and C#. I'm trying to make the sprite of "bird" change when he dies in unity. I tried following some tutorials but it doesn't work, so now I'm just trying to make the sprite change when "A" is pressed, but it still doesn't work. Is it a problem of the script or of the sprite?
Here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeSprite : MonoBehaviour
{
public Sprite deadBird;
void Start()
{
}
void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
GetComponent<SpriteRenderer>().sprite = deadBird;
}
}
}
Here is a screenshot: https://i.stack.imgur.com/PTd9W.png
I think #derHugo is correct, if you have an Animator then that will most likely overwrite any change you try and do. To fix this, you can use that animator and create an animation that is the deadBird. once you have that, connect it to the animator controller from the normal state. in that connection you can create and set a new animator boolean like "isDead" and set it to switch to the dead animation is the bool is true. then change your code from
GetComponent<SpriteRenderer>().sprite = deadBird;
To
anim = gameObject.GetComponent<Animator>() //place this instead of the bird sprite
anim.SetBool("isDead", true); //place this in the if statement
Hope that helps! it's much cleaner to go through the animator as it allows for easier changes as you build your game.
Developing a 3D VR application on Unity using OpenXR (2021.3.11f1).
I'm trying to make it so that a Canvas is hidden until a certain condition is met. That condition is that another 3D object's x position is under 45. Here is my script right now:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OptionsBox : MonoBehaviour
{
public GameObject canvas;
public GameObject playerObj;
void Start()
{
canvas.SetActive(false);
}
void Update()
{
if (playerObj.transform.position.x < 45){
canvas.SetActive(true);
}
}
}
I then made an empty GameObject and inputted the script into there:
However, when I run my scene, the canvas is still displayed. What have I done wrong?
No problem with the script. Problem was the other object started at a x value less than 45, so it was always set to True.
You can add a CanvasGroup to the Canvas and play with the opacity property called Alpha
how do I make it that "Player walks into a trigger and an animation plays once and if the player walks into the same trigger nothing will happen" There are no videos on youtube nor other websites that I know that will explain how to do this.
Im also New to unity and am not the best when doing animations
[EDITED SCRIPT, FIXED ERRORS]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// You should make the name TheSecretWallScript
// This is the general format for classes in C#
public class Thesecretwallscript : MonoBehaviour
{
// needed to change this.
[SerializeField] private Animator anim;
// Start is called before the first frame update
private void OnTriggerEnter(Collider other)
{
// Disables trigger so it doesn't trigger again
other.enabled = false;
// Triggers animation, "AnimTrigger" should refer to the trigger you set
// in your animators settings
Animator anim = GetComponent<Animator>();
anim.SetTrigger("AnimTrigger");
}
}
You need to add a trigger for your animation in the player's Animation Controller.
You could disable the trigger once you walk into it by doing:
private void OnTriggerEnter(Collider other)
{
// Disables trigger so it doesn't trigger again
other.enabled = false;
// Triggers animation, "AnimTrigger" should refer to the trigger you set
// in your animators settings
Animator anim = GetComponent<Animator>();
anim.SetTrigger("AnimTrigger");
}
Makes the animation trigger and destroys the trigger after being touched by the player
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// You should make the name TheSecretWallScript
// This is the general format for classes in C#
public class Thesecretwallscript : MonoBehaviour
{
// needed to change this.
[SerializeField] private Animator anim;
// Start is called before the first frame update
private void OnTriggerEnter(Collider other)
{
// Disables trigger so it doesn't trigger again
other.enabled = false;
// Triggers animation, "AnimTrigger" should refer to the trigger you set
// in your animators settings
Animator anim = GetComponent<Animator>();
anim.SetTrigger("AnimTrigger");
}
}
You are destroying the trigger even before the animation trigger happens.
Try doing the anim.getcomponent in the start method and animation.settrigger and in the next line other enabled =false.
Something like that
I have a mecanim character that I have set up animations for in the animator. I added a 4 triggers to trigger the different animations. My question is, what is the best way to trigger the run animation when a the left/right keyboard button is pressed? What I have now is that the animations keeps getting triggered over and over again because I have the code in an Update(). What can I do so it only gets triggered once and loops through that animation?
I have this working:
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Animator))]
public class AnimationController : MonoBehaviour {
Animator animator;
bool running = false;
void Start() {
animator = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
if(Input.GetButton("Horizontal")) {
if(!running) {
animator.SetTrigger("Run");
running = true;
}
}
if(Input.GetButtonUp("Horizontal")) {
animator.SetTrigger("Idle");
running = false;
}
}
}
But it seems like it is more that what is needed to accomplish this task, especially as the actions and animations list grows.
animator.SetTrigger got an inner boolean that becomes true when you enter the state and becomes false when it comes out of the state so there is no need for bools and if you checkHas Exit Time property in out transition event it would wait for the animation to finish here you can see it in the docs