Question by
DiamondMC102 · Aug 24, 2017 at 02:03 PM ·
animation2d-platformertimekeycode
Quick Question
So I made a script which is attached to a GameObject, that codes when the “G” Key is pressed it will play an animation and enable/disable the Polygon Collider 2D. – How can I make it when the ”G” Key is pressed it plays the animation and at the beginning of the animation it enables the Polygon Collider 2D, and at the end of the animation is disables the polygon Collider
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<PolygonCollider2D>();
myAnimator = GetComponent<Animator>();
}
// 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);
}
}
}
Comment
Answer by SneakyLeprechaun · Aug 24, 2017 at 02:23 PM
You'd probably want to use Events which basically allows functions to be run from an animation. If you can't directly call myPolygonCollider2D.enabled
from an event, you can always put a public function in your script that just says myPolygonCollider2D.enabled = !myPolygonCollider2D.enabled
.
Hope that helps!