Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 MadsBPed · Sep 01, 2014 at 11:12 AM · 2draycasthealth

Selecting a specific target with a 2D raycast

Hello! This is my first time posting here, but I'm really at a loss.

So. First my code for now:

My attack script:

        private void Attack()
         {
             if (Physics2D.Linecast(johnBody.position, johnLeftArm.position, 1 << LayerMask.NameToLayer("Enemy1")) && turnedLeft == true)
             {
                 Debug.Log("hit");
                 Enemy1Health enemy1hp = (Enemy1Health)target.GetComponent("Enemy1Health");
                 enemy1hp.adjustCurrentHealth(-meleeDamage);
                 Debug.Log(enemy1hp.currentHealth);
             }
     
             if (Physics2D.Linecast(johnBody.position, johnRightArm.position, 1 << LayerMask.NameToLayer("Enemy1")) && turnedRight == true)
             {
                 Debug.Log("hit");
                 Enemy1Health enemy1hp = (Enemy1Health)target.GetComponent("Enemy1Health");
                 enemy1hp.adjustCurrentHealth(-meleeDamage);
                 Debug.Log(enemy1hp.currentHealth);
             }
         }   
 


And then my enemy health script:

     void Update () 
     {
         adjustCurrentHealth(0);
     }
 
     public void adjustCurrentHealth (int adjust)
     {
         currentHealth += adjust;
 
         if (currentHealth < 1)
         {
             currentHealth = 0;
             Destroy(gameObject);
         }
 
         if (currentHealth > maxHealth)
         {
             currentHealth = maxHealth;
         }
     }



What is happening is that whenever the 2D raycast hits an enemy, the enemy looses health. BUT! it's not the specific enemy that looses health, it's just the enemy overall. (for example, if the enemy you hit is down to 0 health, ALL of the enemies have 0 health). So I think the answer would be to find the specific target that the raycast is hitting, and adjusting its health. But since I'm really new to using the 2D raycasting, I have to ask you all for help.

I'm not getting the target, other than:

 public GameObject target;

And then dragging the enemy down to the public GameObject. That is where the problem is.

Thank you!

... also, if I'm missing something, just tell me and I'll post it.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Quaker_SDR · Sep 01, 2014 at 11:32 AM

Enemy1Health enemy1hp = (Enemy1Health)target.GetComponent("Enemy1Health");

From where you getting the target?

Comment
Add comment · Show 3 · Share
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
avatar image MadsBPed · Sep 01, 2014 at 11:51 AM 0
Share

See that's the thing, I'm not getting the target, other than:

     public GameObject target;

And then dragging the enemy down to the public GameObject. That is where the problem is.

avatar image Quaker_SDR · Sep 01, 2014 at 12:20 PM 0
Share

Physics2D.Linecast will return RaycastHit2D

 RaycastHit2D target;
 target=Physics2D.Linecast(johnBody.position, johnLeftArm.position, 1 << Layer$$anonymous$$ask.NameToLayer("Enemy1"));
avatar image MadsBPed · Sep 01, 2014 at 03:45 PM 0
Share

Thank you, I think this is enough for me to go on. I can't believe the answer was that simple!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Finding any objects below or above object at ANY POINT? 1 Answer

Getting a Raycast rope to follow its object 0 Answers

2D Character Controller Movement Issues 0 Answers

onGround raycasts return onGround is true while colliding with platform sides 0 Answers

Create clickable GameObjects (JS) 1 Answer


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