- Home /
Question by
CreepyCat2 · Jun 15, 2019 at 02:27 PM ·
enemyraycasthit2dwalking
My Player only gets hurt on one side
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<SpriteRenderer>().enabled = false;
if (time <= 0)
{
invinibly = false;
}
}
else
{
GetComponent<SpriteRenderer>().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;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Enemy walking off platform in 2D mode 1 Answer
2d raycast enemy detection problem. 0 Answers
enemy walking around the room 2d 1 Answer
Stopping AI's animation and movement 0 Answers