Right Now i am working on if the player get hit from the enemys in the level, but im only having it work from one side
heres my code `public enum DIRECTION { UP, DOWN, LEFT, RIGHT }
private bool canMove = true, moving = false;
private int speed = 5, buttoncooldown = 0 ;
private DIRECTION dir = DIRECTION.DOWN;
private Vector3 pos;
public int Health = 3;
public int numberOfHearts;
public bool invinibly = false;
public float time = 3;
public Image[] hearts;
public Sprite emptyheart;
public Sprite fullheart;
void Update()
{
if (invinibly == true)
{
time -= 1 * Time.deltaTime;
GetComponent().enabled = false;
if (time <= 0)
{
invinibly = false;
}
}
else
{
GetComponent().enabled = true;
time = 3;
}
if (Health > numberOfHearts)
{
Health = numberOfHearts;
}
for (int i = 0; i < hearts.Length; i++)
{
if(i < Health)
{
hearts[i].sprite = fullheart;
}
else
{
hearts[i].sprite = emptyheart;
}
if(i < numberOfHearts)
{
hearts[i].enabled = true;
}
else
{
hearts[i].enabled = false;
}
}
buttoncooldown--;
if (canMove)
{
pos = transform.position;
move();
}
if (moving)
{
if (transform.position == pos)
{
moving = false;
canMove = true;
move();
}
transform.position = Vector3.MoveTowards(transform.position, pos, Time.deltaTime * speed);
}
//====RayCasts====//
RaycastHit2D hitup = Physics2D.Raycast(transform.position, Vector2.up, 1);
RaycastHit2D hitdown = Physics2D.Raycast(transform.position, Vector2.down, 1);
RaycastHit2D hitright = Physics2D.Raycast(transform.position, Vector2.right, 1);
RaycastHit2D hitleft = Physics2D.Raycast(transform.position, Vector2.left, 1);
// Only when i go on top of the enemy i get hurt
if (hitdown.collider.tag == "Enemy")
{
if (invinibly == false)
{
Health -= 1;
invinibly = true;
}
}
if (hitup.collider.tag == "Enemy")
{
if (invinibly == false)
{
Health -= 1;
invinibly = true;
}
}
if (hitleft.collider.tag == "Enemy")
{
if (invinibly == false)
{
Health -= 1;
invinibly = true;
}
}
if (hitright.collider.tag == "Enemy")
{
if (invinibly == false)
{
Health -= 1;
invinibly = true;
}
}
}
↧