Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

107 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

How do I make it so that a walking animation plays for the enemy AT THE SAME TIME it is moving toward the player? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges