- Home /
Animation Problems
I made a script that enables/disables the Polygon Collider 2D component when the ''g'' key is pressed. This script is attached to a GameObject called Indingo(The Player). So when the "g" key is pressed the script enables or disables the Polygon Collider 2D and plays the animation.
What I want: I want the Polygon Collider 2D to be disabled at the beginning. Meaning I want the Polygon Collider 2D to be disabled at the start of the game, and for the Polygon Collider 2D to be enabled when the animation starts and be disabled when the animation ends all when the ''g'' key is pressed.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Indingo_Lava_Burst_Activator_Deactivator : MonoBehaviour {
private PolygonCollider2D myPolygonCollider2D; private Animator myAnimator;
// Use this for initialization void Start() { myPolygonCollider2D = GetComponent(); myAnimator = GetComponent(); }
// Update is called once per frame void Update() { if (Input.GetKeyUp(KeyCode.G)) { myPolygonCollider2D.enabled = !myPolygonCollider2D.enabled; myAnimator.SetBool("attack", true); } else { myAnimator.SetBool("attack", false); } } }
Answer by AbominationGames · Aug 20, 2017 at 05:40 AM
Okay so I would do something like disable the collider initially. (You can do this manually or through your code in your awake or start functions) Then you will need to do something like:
// PSEUDO CODE IF > COLLIDER is ACTIVE IN HEIRARCHY THEN DO > (Your Current If Statement) ELSE > Return;
https://docs.unity3d.com/ScriptReference/GameObject-activeInHierarchy.html
http://answers.unity3d.com/questions/1063167/should-i-be-using-activeinhierachy-or-activeself-c.html
Your answer
Follow this Question
Related Questions
A problem regarding to player animation with checkpoint in 2D platformer 0 Answers
Animator and Scripting problems. Help !! 1 Answer
Simple Animation and Scripting Problem. 1 Answer
timer that can be reset 1 Answer
2D platformer - character repeats some attacks after you stop pressing attack 0 Answers