Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 SepM · Jul 21, 2015 at 01:57 AM · nullreferenceexceptiondistancehitboxfighting

Stop Moving at a Certain Distance from a Hit Box

I'm having a problem with my fighting game which has to do with an interaction between two objects: a guarding character and an attacking "hit box." What I want to do is have the character stop moving when a hit box prefab has its type variable set to "attack". I have no idea how to do that though. Is there a way that I can not only find another object, calculate distance, and also access its variables or is there a better way to do it? I've tried starting with:

 hitBox = GameObject.FindWithTag("hitBox");
          if(hitBox.GetComponent("hitBox").GetType == "attack" || hitBox.GetComponent("hitBox").GetType == "projectile"){
              Debug.Log(GameObject.FindWithTag("hitBox").transform.position);
          }

But this returns an NullReferenceException error and nothing is printed onto my debug log. I've also tried just GameOjbect.Find, but that didn't work either. My question might be a little confusing, so even if you're unsure exactly what I'm trying to ask, any help is appreciated.

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
1
Best Answer

Answer by zebra6595 · Jul 21, 2015 at 02:51 AM

you could try putting a collider on your character that's the size of the area or distance you want and use it as a trigger.

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 SepM · Jul 21, 2015 at 02:45 PM 0
Share

That's a good idea, but I'm already using a collider for actual collisions. Thanks for your input!

avatar image zebra6595 · Jul 21, 2015 at 07:38 PM 1
Share

you could also use a raycast and check the tag of what it hits and the distance something like this for 3d if (Physics.Raycast (new Vector3(0,0,0),Vector3.forward, out hit, 100)&& hit.transform.tag == "player"&& hit.distance<=stopdistance) { stop player here } for 2d

if (Physics2D.Raycast (new Vector2(0,0),Vector2.right, out hit, 100)&& hit.transform.tag == "player"&& hit.distance<=stopdistance) { stop player here }

avatar image SepM · Jul 21, 2015 at 11:30 PM 0
Share

Actually, I came up with an idea and adapted your original answer. What I did was make the hit box create another box that would act as its second box collider that lets a guarding character to stop moving when it gets close. $$anonymous$$y code went like this:

 //This code pertains to the guarding character.
 var rangeOf : Transform;
 var inRange : boolean;
 
 function Update () {
     if(rangeOf == null && (type == "guarding" || type == "neutral")){
         rangeOf = null;
         inRange = false;
     }
 }
 
 //When within the range of a guardRange, do not move.
 function OnTriggerStay(range : Collider) {
     if(range.gameObject.tag == "guardRange" && type == "guarding"){
 //Saves the extra box as a variable so that eventually when it disappears, or becomes null, inRange will be false (as shown in the Update function).
         rangeOf = range.transform;
         inRange = true;
     }
 }
 //And just in case a guarding character is at the edge of the box and exits it (due to drag), inRange will also be false.
 function OnTriggerExit(range : Collider){
     if(range.gameObject.tag == "guardRange" && (type == "guarding" || type == "neutral")){
         rangeOf = null;
         cameFrom.GetComponent("$$anonymous$$ove").inRange = false;
     }
 }

Thank you very much for your help, zebra6595!

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

23 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

Related Questions

Hit detection for Fighting Games? 1 Answer

NullReferenceException: Object reference not set to an instance of an object 1 Answer

Drawing Rectangles in 2D for hitboxes: fighting game 1 Answer

A Fighting Games Hit boxes 1 Answer

Vector3.Distance giving me NullReferenceException every time 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