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
1
Question by Grady · Dec 21, 2011 at 03:47 AM · collisionplayerenemy

Collision issue with player and enemy

Hey guys, I am having a bit of an issue here.

What I have setup is a first person controller, and many enemies that follows the first person controller. I have this script on the first person controller:

 var health = 4;
 var damage = 1;
 var wait_time = 2;
 var lock = 0;
     function OnCollisionEnter(hit: Collision){
         if(hit.gameObject.tag == "enemy" && lock == 0){
            StartCoroutine("DecreasingHealth");
         }
     }
 
     function DecreasingHealth(){
         lock=1;
         health=health-damage;
         yield WaitForSeconds(wait_time);
         lock=0;
     }
 
     function Update(){
        if(health == 0){
           Destroy(gameObject);
        }
     }

Except....., the collision won't work. On the first person controller, I have the character controller which automatically has a collider on it and I have a non kinematic rididbody. I have the same non kinematic rididbody on the enemies, but I have added a capsule collider which has "Is Trigger" uncheckedd. There are also no errors for this script in the console....

Can anyone see what's wrong here???

Thanks!

-Grady

Comment
Add comment · Show 11
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 ByteSheep · Dec 21, 2011 at 02:06 PM 0
Share

It sounds like the enemy has got to be directly touching the player, perhaps try adding a box collider (should be a bit bigger than player) and setting it to a trigger. Then check for OnTriggerEnter ins$$anonymous$$d of OnCollisionEnter, this could perhaps solve the problem. If it doesn't then perhaps try checking whether the collision is being detected through using Debug.Log(); I guess the problem could be that the two capsule colliders aren't colliding properly, so my suggestion would be to try adding the box collider and make sure that the enemy is within range of the collider.

avatar image Grady · Dec 22, 2011 at 01:00 AM 0
Share

ok, thankyou, i will try that now!!!!!!!!!

avatar image Grady · Dec 22, 2011 at 04:10 AM 0
Share

It won't work for some reason, I keep getting this error:

Actor::update$$anonymous$$assFromShapes: Compute mesh inertia tensor failed for one of the actor's mesh shapes! Please change mesh geometry or supply a tensor manually! UnityEngine.Object:Instantiate(Object, Vector3, Quaternion) $:$$anonymous$$oveNext() (at Assets/Scripts/gun.js:42) UnityEngine.$$anonymous$$onoBehaviour:StartCoroutine_Auto(IEnumerator) gun:Update() (at Assets/Scripts/gun.js:19)

I have searched the error, but I don't have any planes with mesh colliders on them.....

on line 42 of gun.js, it is just where I am instantiating the bullet prefab whenever the player fires the gun... if you want me to show gun.js as well, I can do that.....

-Grady

avatar image ByteSheep · Dec 22, 2011 at 09:28 AM 1
Share

The script above will be able to detect whether the player is ai$$anonymous$$g at the enemy and whether the player is shooting. The script is checking whether you are ai$$anonymous$$g and shooting at an object with the tag "shootable" which can be assigned to any object. This is done in the inspector where you can create a new tag and name it shootable. Another option would be to change the line

if(hit.collider.gameObject.tag == "shootable")
to
if(hit.collider.gameObject.name == "Enemy").

This is maybe the simpler option for now, but make sure that you set the "Enemy" part to whatever the enemies name is in the hierarchy view. Then you can display in the console whether the enemy was hit by using Debug.Log(); . If you have already got a variable for life of the enemy, then you can subtract from that variable.. Here is an updated version of the script:

var hit : RaycastHit; var EnemyLife : float = 10;

function Update () {

var fwd = transform.TransformDirection (Vector3.forward);

if (Physics.Raycast (transform.position, fwd, hit, 100)) {

 //Here we check if we hit an enemy, be sure to set "Enemy" to 
 //whatever the enemies name is in the hierarchy view!!

 if(hit.collider.gameObject.name == "Enemy")
 {

   //If player shoots while ai$$anonymous$$g at enemy..
   if(Input.GetButtonDown("Fire1"))
   {

   //subtract from enemy life
   EnemyLife -= 1;
   
   //Display message in console
   Debug.Log("Bullet hit enemy");

   }

 }

}

}

avatar image ByteSheep · Dec 23, 2011 at 07:29 AM 1
Share

That's probably the best way to do it, you could also make the health variable static:

static var health : float = 10;

and then you can access that variable from any script using the script name:

zombie.health--;

with hit.collider.GetComponent(zombie).health--; you are already doing the same thing though, I just thought I'd mention it, glad you got it working ;)

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Player Dies when Collide with enemy 2 Answers

Player and enemy collision 1 Answer

Only targeting the targets root of vector 3 and not colliding with its mesh 1 Answer

Lose health on collision 1 Answer

Player not getting destroyed when touched by enemies 4 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