Scriptum/Assets/SwitchSprite.cs

58 lines
1.3 KiB
C#
Raw Normal View History

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SwitchSprite : MonoBehaviour
{
public bool inRange = false;
public Sprite[] sprites;
private List<int> availableSprites = new List<int>();
2023-01-02 15:02:08 +01:00
public int current;
// Start is called before the first frame update
void Start()
{
availableSprites.Add(1);
}
// Update is called once per frame
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "Player")
{
inRange = true;
}
}
private void OnTriggerStay2D(Collider2D collision)
{
if (collision.tag == "Player")
{
inRange = true;
}
}
private void OnTriggerExit2D(Collider2D collision)
{
inRange = false;
}
void Update()
{
2023-01-02 15:02:08 +01:00
if (inRange == true)
{
2023-01-02 15:02:08 +01:00
if (Input.GetKeyDown(KeyCode.E) && current != 1)
{
GetComponent<SpriteRenderer>().sprite = sprites[0];
2023-01-02 15:02:08 +01:00
current = 1;
}
2023-01-02 15:02:08 +01:00
else if (Input.GetKeyDown(KeyCode.E) && current == 1)
{
GetComponent<SpriteRenderer>().sprite = sprites[1];
current = 0;
}
}
}
}