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 FizzyBear · Apr 19, 2012 at 04:42 AM · animationjavascriptcollisionshooting

Enemy not getting hit (Collider issue)

Right, my main character has a spawnpoint which is a child of him, that spawnpoint creates a sphere consisting of a box collider. Now the problem I'm having is that when I shoot that sphere, it doesn't collide with the enemy. Each enemy has 2 lives (set in the inspector). This is my script.

 // variable to store the enemy ragdoll
 public var ragdoll:GameObject;
 public var enemyLives:int = 0;
 
 function onCollisionEnter (enemyhit:Collision)
 {
     print("Testing collision");
     if (enemyhit.name == "Enemy") {
         GetComponent(enemyShot).enemyLives--;
         if (enemyLives <= 0)
         Instantiate(ragdoll, transform.position, transform.rotation);        
         animation.CrossFade("die");
         Destroy(this.gameObject);
     }    
 }

Each of the enemy consist of a Character Controller. I've put RigidBody, box colliders etc on every single one of them + the child, the sphere still doesn't collide. Did the same for the sphere, nothing is colliding. If anyone can help on this, it'd be greatly appreciated.

The:

  print("Testing collision");
 

Doesn't work either "/ What am I missing? D:

Comment
Add comment · Show 2
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 fafase · Apr 19, 2012 at 05:02 AM 0
Share

Also your function is written wrong OnCollisionEnter, you need a cap on On.

avatar image FizzyBear · Apr 19, 2012 at 05:04 AM 0
Share

Sorry, I typed the code up on this ins$$anonymous$$d of copying and pasting. $$anonymous$$y bad :P

2 Replies

· Add your reply
  • Sort: 
avatar image
-1

Answer by maroonrs2 · Apr 19, 2012 at 05:42 AM

     // variable to store the enemy ragdoll
 public var ragdoll:GameObject;
 public var enemyLives:int = 0;
 
 function onCollisionEnter (enemyhit:Collider)
 {
     print("Testing collision");
     if (enemyhit.CompareTag("Enemy") {
        GetComponent(enemyShot).enemyLives--;
        if (enemyLives <= 0)
        Instantiate(ragdoll, transform.position, transform.rotation);     
        animation.CrossFade("die");
        Destroy(this.gameObject);
     }  
 }

ok what you did wrong was try to find its name. Make a tag. then assign it. and its not :collision its :collider. did you get any output after you changed it to mine?

Comment
Add comment · Show 7 · 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 fafase · Apr 19, 2012 at 05:48 AM 2
Share

I see three things wrong on your answer, OnCollisionEnter needs a cap, and OnCollisionEnter is passed the Collision class not coliider, collider is for Trigger. Also, if you don't put the bracket properly on the if(enemyLives

avatar image FizzyBear · Apr 19, 2012 at 05:49 AM 0
Share

Tried both your answers, I still got the same effect, either I've done my Colliders + RigidBodies wrong? Or I'm just missing something.

avatar image maroonrs2 · Apr 19, 2012 at 06:02 AM 0
Share

he wants it to trigger... the whole point of it is to get hit and then go away. He has his character. Its even got a box collider in a sphere! Shows how much you actually see whats going on.

avatar image syclamoth · Apr 19, 2012 at 06:03 AM 1
Share

Yes, but your script still has problems. Please only post code that you have verified to work.

avatar image maroonrs2 · Apr 19, 2012 at 06:05 AM 0
Share

He also needs a capital O on his function. He also needs to switch functions.

Show more comments
avatar image
0

Answer by fafase · Apr 19, 2012 at 06:51 AM

OK here is my setups, I made a simplified version, I have a sphere which act as your bullet, it has rigidbody, sphere collider (no IsTrigger) and just a movement script. Then i have another sphere that acts as your enemy with CC, rigidbody (IsKinematic) and a sphere collider (no IsTrigger).

It has only this script:

  var lives = 5;
     function OnCollisionEnter(other:Collision){
         if(other.gameObject.tag =="Sphere")   
            Debug.Log("Collision");
            lives-=1;} 

And it works. I have not altered any other members of it all, just add components and that is it. MAybe you could add a collider to your enemy. As you rigidbody is set to IsKinematic, it won't interfere as IsKinematic disables the collider.

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

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

6 People are following this question.

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

Related Questions

Can someone help me with my script? 0 Answers

Shooting length 1 Answer

[SOLVED] Shooting fail (Due to camera setup) 2 Answers

Attack animation Wont Play 1 Answer

Destroy method returning null. 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