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 /
  • Help Room /
avatar image
0
Question by simplicitydown · Dec 12, 2015 at 04:38 PM · 2dmovementraycasttop-downmelee attack

Raycast is hitting enemy even when not in contact with it?

My melee script uses raycasting in 8 directions in conjuction with my movement script to allow a player-cube to attack in 8 directions and take health off of enemies successfully. The problem is that once an enemy is hit once, then they will keep on losing health when the attack button is pressed even when they are not in contact with any of the attacking raycasts. It is completely baffling:

 public EnemyHealth enemyScript;

 public Vector2 speed = new Vector2 (50, 50);
 private Vector2 movement;
 private Rigidbody2D playerRigid;

 public Transform lineStart, rightLineEnd, leftLineEnd, upLineEnd, downLineEnd, upRightLineEnd, upLeftLineEnd, downRightLineEnd, downLeftLineEnd;
 
 RaycastHit2D whatIHit;
 public bool moveRight = false, moveLeft = false, moveUp = false, moveDown = false;


 void Start ()
 {
     playerRigid = GetComponent<Rigidbody2D> ();
 }

 void Update ()
 { 
 
     float inputX = Input.GetAxis ("Horizontal");
     float inputY = Input.GetAxis ("Vertical");

     movement = new Vector2 (
         speed.x * inputX,
         speed.y * inputY);


      if (movement.x > 0f) {
         moveRight = true; moveLeft = false; moveUp = false; moveDown = false;
         Debug.DrawLine (lineStart.position, rightLineEnd.position, Color.green);
     } 

     if (movement.x < 0f) {
         moveRight = false; moveLeft = true; moveUp = false; moveDown = false;
         Debug.DrawLine (lineStart.position, leftLineEnd.position, Color.green);
     } 
         
     if (movement.y < 0f) {
         moveRight = false; moveLeft = false; moveUp = false; moveDown = true;
         Debug.DrawLine (lineStart.position, downLineEnd.position, Color.green);

     } if (movement.y > 0f) {
         moveRight = false; moveLeft = false; moveUp = true; moveDown = false;
         Debug.DrawLine (lineStart.position, upLineEnd.position, Color.green);
     
     }


     if (movement.x > 0f  && movement.y > 0f) {
         moveRight = true; moveLeft = false; moveUp = true; moveDown = false;
         Debug.DrawLine (lineStart.position, upRightLineEnd.position, Color.green);
     } 

     if (movement.x < 0f  && movement.y > 0f) {
         moveRight = false; moveLeft = true; moveUp = true; moveDown = false;
         Debug.DrawLine (lineStart.position, upLeftLineEnd.position, Color.green);
     } 

     if (movement.x > 0f  && movement.y < 0f) {
         moveRight = true; moveLeft = false; moveUp = false; moveDown = true;
         Debug.DrawLine (lineStart.position, downRightLineEnd.position, Color.green);
     } 

     if (movement.x < 0f  && movement.y < 0f) {
         moveRight = false; moveLeft = true; moveUp = false; moveDown = true;
         Debug.DrawLine (lineStart.position, downLeftLineEnd.position, Color.green);
     } 

     Debug.DrawLine (blockRight1.position, blockRight2.position, Color.green);
     Debug.DrawLine (blockLeft1.position, blockLeft2.position, Color.green);
     playerRigid.velocity = movement;
     Raycasting ();
     RaycastUse ();

 }
 
 void FixedUpdate ()
 {
     playerRigid.velocity = movement;

 }
     

 void Raycasting()
     {
             
             
     if (Physics2D.Linecast (lineStart.position, rightLineEnd.position, 1 << LayerMask.NameToLayer ("Enemy1")) && moveRight == true) {
         whatIHit = Physics2D.Linecast (lineStart.position, rightLineEnd.position, 1 << LayerMask.NameToLayer ("Enemy1"));
         moveRight = false;
     }

     else if (Physics2D.Linecast (lineStart.position, leftLineEnd.position, 1 << LayerMask.NameToLayer ("Enemy1"))  && moveLeft == true) {
         whatIHit = Physics2D.Linecast (lineStart.position, leftLineEnd.position, 1 << LayerMask.NameToLayer ("Enemy1"));
         moveLeft = false;

         }
     else if (Physics2D.Linecast (lineStart.position, upLineEnd.position, 1 << LayerMask.NameToLayer ("Enemy1"))  && moveUp == true) {
         whatIHit = Physics2D.Linecast (lineStart.position, upLineEnd.position, 1 << LayerMask.NameToLayer ("Enemy1"));

             }

     else if (Physics2D.Linecast (lineStart.position, downLineEnd.position, 1 << LayerMask.NameToLayer ("Enemy1"))  && moveDown == true) {
         whatIHit = Physics2D.Linecast (lineStart.position, downLineEnd.position, 1 << LayerMask.NameToLayer ("Enemy1"));

     }

     else if (Physics2D.Linecast (lineStart.position, upRightLineEnd.position, 1 << LayerMask.NameToLayer ("Enemy1"))  && moveUp == true && moveRight == true) {
         whatIHit = Physics2D.Linecast (lineStart.position, upRightLineEnd.position, 1 << LayerMask.NameToLayer ("Enemy1"));

     }

     else if (Physics2D.Linecast (lineStart.position, upLeftLineEnd.position, 1 << LayerMask.NameToLayer ("Enemy1"))  && moveUp == true && moveLeft == true) {
         whatIHit = Physics2D.Linecast (lineStart.position, upLeftLineEnd.position, 1 << LayerMask.NameToLayer ("Enemy1"));

     }

     else if (Physics2D.Linecast (lineStart.position, downRightLineEnd.position, 1 << LayerMask.NameToLayer ("Enemy1"))  && moveDown == true && moveRight == true) {
         whatIHit = Physics2D.Linecast (lineStart.position, downRightLineEnd.position, 1 << LayerMask.NameToLayer ("Enemy1"));

     }

     else if  (Physics2D.Linecast (lineStart.position, upLeftLineEnd.position, 1 << LayerMask.NameToLayer ("Enemy1"))  && moveUp == true && moveLeft == true) {
         whatIHit = Physics2D.Linecast (lineStart.position, downLeftLineEnd.position, 1 << LayerMask.NameToLayer ("Enemy1"));
 
     }
 
 }

     void RaycastUse (){

     if (Input.GetKeyDown (KeyCode.E) && mUpgrade1 == false) {
         whatIHit.collider.gameObject.GetComponent<EnemyHealth>().DamageE1();

     }
         
 
     }
 }



I've re-wrote it a bunch of times and it always does the same thing I have no idea what I am not understanding. Can't do anything till this is solved...Really appreciate any help!

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

2 Replies

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

Answer by robin-theilade · Dec 12, 2015 at 05:04 PM

@simplicitydown, whatIHit is never assigned back to default(RaycastHit2D) so as soon as the line cast have hit something it will remember that forever.

You need to assign the whatIHit to default(RaycastHit2D) when none of the other cases in your Raycasting() method is met.

As RaycastHit2D is a struct it will never be null so I would advise you to test if whatIHit.collider is null to avoid null reference exceptions.

Comment
Add comment · 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
0

Answer by simplicitydown · Dec 12, 2015 at 06:32 PM

@robin.theilade Awesome! Well I got it to work just using

 } else {
             whatIHit = default(RaycastHit2D);
         }

At the end of the Raycasting method.

Comment
Add comment · Show 1 · 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 robin-theilade · Dec 13, 2015 at 09:42 AM 0
Share

Good work :)

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

48 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

Related Questions

Checking for Raycast distances not working as expected. 0 Answers

Feild of view only wokring in specific areas of the scene 0 Answers

Boss AI Help in a 2D Platform game 0 Answers

I need help for a mobile joystick 0 Answers

Object drifting to left or right without reason 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