Question by
unity_jeQaoDwWpy4zaQ · May 29 at 07:30 AM ·
unity 2d2d animation
I need help with Door animations
Hi I'm creating my first game and I've been learning A LOT with this project but i'm stuck now. I was doing a key and door system that I found from Codemonkey. and it all worked untill i decided to change the colors. since i changed the colors now the 3 doors wont share the same animation. the problem is that i cant figure out how to code:
"if the green door is opened, play the 'GreenDoorOpen' animation;"
"if the blue door is opened, play the 'BlueDoorOpen' animation;"
"if the red door is opened, play the 'RedDoorOpen' animation; "
The Door Script:
public class KeyDoor : MonoBehaviour
{
public Animator anim;
[SerializeField] private Key.KeyType keyType;
public Key.KeyType GetKeyType()
{
return keyType;
}
public void OpenDoor()
{
anim.Play("DoorOpen");
}
The KeyHolder Script:
public class KeyHolder : MonoBehaviour
{
private List<Key.KeyType> keyList;
private void Awake()
{
keyList = new List<Key.KeyType>();
}
public void Addkey(Key.KeyType keyType)
{
Debug.Log("Added Key: " + keyType);
keyList.Add(keyType);
}
public void Removekey(Key.KeyType keyType)
{
keyList.Remove(keyType);
}
public bool Containskey(Key.KeyType keyType)
{
return keyList.Contains(keyType);
}
private void OnTriggerEnter2D(Collider2D collider)
{
Key key = collider.GetComponent<Key>();
if (key != null)
{
Addkey(key.GetKeyType());
Destroy(key.gameObject);
}
KeyDoor keyDoor = collider.GetComponent<KeyDoor>();
if (keyDoor != null)
{
if (Containskey(keyDoor.GetKeyType()))
{
//Currently holding Key to open this door
Removekey(keyDoor.GetKeyType());
keyDoor.OpenDoor();
}
}
}
}
The key script:
public class Key : MonoBehaviour
{
[SerializeField] private KeyType keyType;
public enum KeyType
{
Red,
Green,
Blue
}
public KeyType GetKeyType()
{
return keyType;
}
}
Comment