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");
}
Related
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
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
I am trying to create a script for my enemy turret, but it is not going well. I have a couple animations of the turret being activated and deactivated. What I need is that based on the distance from the player, it plays either animation. So once it moves inside the detection radius it plays the activation animation and once it is outside it plays the deactivation animation. Most of the other ways I try require me to create an Animation Controller, which I have little experience in using. I want a simple way to play one animation once it is inside and play a different one when it is outside. I think there was a way to store the animation clip in the script, and then play it. I have attached my current script, so you know what I mean.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyTurret : MonoBehaviour
{
public GameObject Player;
public float DistanceToPlayer;
public float DetectionRadius = 75;
// Start is called before the first frame update
void Start()
{
Player = GameObject.FindGameObjectWithTag("PlayerTank");
}
// Update is called once per frame
void Update()
{
DistanceToPlayer = Vector3.Distance(transform.position, Player.transform.position);
if (DistanceToPlayer<=DetectionRadius)
{
Debug.Log("Within Radius");
}
if (DistanceToPlayer >= DetectionRadius)
{
Debug.Log("Outside Radius");
}
}
}
Calculating and checking the distance of the player for every update() is not ideal. It will work, but it will do more work than it needs to when the player isn't even near it. Its not efficient.
What you may want to do if your player is a Rigidbody, is add a SphereCollider to the turret, set isTrigger=true, set the radius to be your detection radius, and handle the OnTriggerEnter() and OnTriggerExit() events to play or stop animations.
You can also add two public Animiation objects to the script, drag and drop your animations in the editor, then you can use animation.Play() and .Stop() etc. to control the animations.
Something similar to this. Not tested fully, but you can get the idea.
public float detectionRadius = 75;
public Animation activateAnimation;
public Animation deactivateAnimation;
void Start()
{
SphereCollider detectionSphere = gameObject.AddComponent<SphereCollider>();
detectionSphere.isTrigger = true;
detectionSphere.radius = detectionRadius;
detectionSphere.center = Vector3.zero;
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "PlayerTank")
{
activateAnimation.Play();
}
}
private void OnTriggerExit(Collider other)
{
if (other.gameObject.tag == "PlayerTank")
{
deactivateAnimation.Play();
}
}
Your animations must not loop otherwise you will have to add more logic to check if animation.isPlaying and do your own animation.Stop() etc.
Heirarchy structure
using UnityEngine;
using System.Collections;
public class Ball : MonoBehaviour {
private Rigidbody rigidbody;
public Vector3 LaunchVelocity;
private AudioSource audiosource;
// Use this for initialization
void Start ()
{
GameObject.Find("Touch Panel").SetActive(false);
rigidbody = GetComponent<Rigidbody>();
rigidbody.isKinematic = true;
// disable touch control
audiosource = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
}
public void LaunchBall(Vector3 passedvelocity)
{
rigidbody.velocity = passedvelocity;
}
public void DropBall() // This is attached to a button
{
rigidbody.isKinematic = false;
GameObject.Find("Touch Panel").SetActive(true);
}
}
As you can see above I have disable "Touch Panel" at start function. But on DropBall function ( remember it is attached to a button), I have set it to active but it is not working.Can anyone help me with this.Thanks.
EDIT:- This script is attached on a "Ball". and ball is attached on a Button.
"Touch Panel" is child of Canvas.
Your problem is that when you disable your Touch Panel , GameObject.Find will not find it so you won't be able to enable it again.
From the documentation:
This function only returns active GameObjects.
You need to setup your Touch Panel as a public GameObject myPanel and assign it in the inspector then you can enable and disable it.
myPanel.SetActive(false);
myPanel.SetActive(true);
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