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 dudedude123 · Sep 29, 2014 at 09:28 AM · c#collisionfighting

Fighting collision

I'm making a fighting game and I had set up a code

 public class Fight : MonoBehaviour {
 
 
     public float Damage=60;
     public List<Transform> HitList;
     private bool hit;
     public bool YouWon;
     public bool ko;
     private bool dealdamage;
     internal Animator animator; //stores the animator component
     float v; //vertical movements
     float h; //horizontal movements
     float straight; 
     float hook;
     float block;
     float body;
     float jump;
     float left;
     float right;
     float high;
     float wide;
 
 
 
     
     void  Start (){
         animator = GetComponent<Animator>(); //assigns Animator component when we start the game
 
     }
     
     void  Update (){
         v = Input.GetAxis("Vertical");
         h = Input.GetAxis("Horizontal");
         straight = Input.GetAxis("Straight");
         hook = Input.GetAxis("Hook");
         high = Input.GetAxis("Guard Height");
         wide = Input.GetAxis("Guard Width");
         block = Input.GetAxis("Block");
         body = Input.GetAxis("Body");
         TurnLeft();
         TurnRight();
         Jump();
 
         Stats php=(Stats)GetComponent("Stats");
         if(php){
             if(php.CurrentHealth<=0)ko=true;
         }
         //LETS DO SOME KILLIN!
         if(hit){
             
 
             if(HitList.Count>0&dealdamage){
                     int    ls=HitList.Count;
                     for (int i = 0; i < ls; i++){
                     Stats hp=(Stats)HitList[i].transform.GetComponent("Stats");
                         
                         hp.CurrentHealth=hp.CurrentHealth-Damage;
                         if(hp.KO){}
                     }
                     dealdamage=false;
                 }
             }
         }
 
     
     void  FixedUpdate (){
         
         //set the "Walk" parameter to the v axis value
         animator.SetFloat ("Walk", v);
         animator.SetFloat ("Turn", h);
         animator.SetFloat ("Straight", straight);
         animator.SetFloat ("Hook", hook);
         animator.SetFloat ("Block", block);
         animator.SetFloat ("Body", body);
         animator.SetFloat("Turn Left", left);
         animator.SetFloat("Turn Right", right);
         animator.SetFloat("Guard Height", high);
         animator.SetFloat("Guard Width", wide);
         animator.SetFloat("Jump", jump);
     }
     
     void  TurnLeft (){
         if(Input.GetButton("Turn Left")) {
             left = 0.2f;
         }
         else {
             
             left = 0.0f;
         }
         
     }
     
     void  TurnRight (){
         if(Input.GetButton("Turn Right")) {
             right = 0.2f;
         }
         else {
             
             right = 0.0f;
         }
         
     }
     
     void  Jump (){
         if(Input.GetButton("Jump")) {
             jump = 0.2f;
         }
         else {
             
             jump = 0.0f;
         }
         
     }
 
     void OnCollisionEnter(Collider other){
         //ADD AIS TO LIST TO INFLICT DAMAGE IF IN RANGE
         Health AI=(Health)other.transform.GetComponent("Health");
         if(AI){
             HitList.Add(other.transform);
             
         }
     }
     
     void OnCollisionExit(Collider other){
         //REMOVE FROM LIST WHEN OUT OF RANGE
         Health AI=(Health)other.transform.GetComponent("Health");
         if(AI){
             HitList.Remove(other.transform);
             
         }
     }
     
     void OnGUI(){
         //HEALTH BAR AND AI COUNT
         Health php=(Health)GetComponent("Health");
         if(php){
             float hpp=php.CurrentHealth;
             GUI.Button(new Rect(0, 30, 300, 26), "Health: "+hpp);
         }
 
         //YOU WON!
         if(YouWon){
             GUI.Box(new Rect(200, 200, 330, 260), "Congratulations!  You have defeated all the Evil Skellies!");
             if(GUI.Button(new Rect(310, 400, 120, 26), "Continue Playing"))YouWon=false;
         }
         
     }
 
 
 
 }

How can I get the fighter to detect enemy collision?

Comment
Add comment · Show 3
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 christoph_r · Sep 29, 2014 at 07:09 PM 2
Share

First of all, consider splitting all those things into multiple separate scripts. You're probably going to add a few more things and the more you add, the more confusing the entire script gets. Splitting it into a few different scripts with clear defined purposes will help you keep control of your scripts.

The second thing is, what exactly do you mean by "detect enemy collision"?

avatar image dudedude123 · Sep 29, 2014 at 11:49 PM 0
Share

When the fighter throws the punch I want the enemy model to the detect the collision.

avatar image christoph_r · Sep 30, 2014 at 12:45 PM 0
Share

For more information on when which method gets called, see this site.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by raja-bala · Sep 30, 2014 at 12:17 AM

Too much code to look at :) So, add a few Debug.Log statements in the OnCollision* methods and see if they get called. If not, you have an issue in your collider/rigidbody setup. The gist is that Unity will NOT detect a collision b/w two objects with colliders where none of them have a rigidbody component. If you want to have it that way, you'll have to explore the OnTrigger* methods.

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 dudedude123 · Oct 04, 2014 at 02:49 AM 0
Share

Ho do I keep the player balanced with rigidbody?

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to call OnTriggerEnter once 5 Answers

2D box colliders not touching but are colliding, how to fix? 0 Answers

Weapon System with collide detection (Helps with script pls)!!! 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