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 mitch1gibson · May 23, 2017 at 07:44 PM · c#collidervrcollision detection

Collider Problems on melee weapon

I am messing around with VR and currently have a pickaxe with sphere colliders at the end as well as a cube with a box collider and a script;

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Rock : MonoBehaviour
 {
     int health = 5;
    
     public void OnCollisionEnter(Collision collision)
     {
         print("collison detected");
         if (collision.gameObject.tag == "PickAxe")
         {
             print("Pick Axe hit");
             health -= 1;
             if (health <= 0)
             {
                 print("Object Destroyed");
             }
         }
     }
 }


none of the print commands are being triggered when I hit the box and can't figure out why. Any help is appreciated

Comment
Add comment · Show 4
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 Kishotta · May 23, 2017 at 09:41 PM 0
Share

Is the object set to be a trigger?

avatar image SpaceManDan · May 24, 2017 at 11:36 AM 2
Share

Don't use collision stay. Both triggerStay and collisionStay are horrible hogs on resources. The garbage collector will constantly be dumping data if you use them. Stay is bad practice for a billion reasons. Use stay when no other option presents itself and god (or gods) himself/themselves have appeared and told you to use it. Otherwise, avoid it. Don't believe me?? Get out the profiler and see what I mean.

Likely, you need to add rigidBodies to the object you are trying to collide with. This keeps it in time with the physicsEngine (FixedUpdate). Do an experiment and add a rigidBody to the object you mean to collide with and see if you get it to detect the collision. Don't ask me why, it would take to long to explain, but putting a rigidbody on a gameobject makes it 100% collision ready. Trigger or Collider.

Also, if you want the object to stay in one place, disable gravity and check $$anonymous$$inematic. $$anonymous$$inematic means that it can only be moved without forces of physics. I.$$anonymous$$ Gravity, Force, Impulse, etc. So making a rigidbody that is kinematic should keep it located in the place it was spawned. If it is a child of another moving object it will keep with it as it moves. 10000 hours of study will help you understand how to use rigidbodies with physics, but for now, just make an object that has a rigidbody that is $$anonymous$$inematic and smack it with that pickaxe. BA$$anonymous$$! you're good to go.

avatar image mitch1gibson SpaceManDan · May 24, 2017 at 05:36 PM 0
Share

Thanks for the help, this doesnt seem to work perhaps its something to do with the pick on a vr controller?

avatar image SpaceManDan mitch1gibson · May 24, 2017 at 09:03 PM 0
Share

it is important to note that the pick colliders also will need kinematic rigid bodies as well.

2 Replies

· Add your reply
  • Sort: 
avatar image
-1

Answer by MrCrumbl3d · May 24, 2017 at 07:59 AM

Try OnCollisionStay. And make sure that your tag is totally the same in your object that you want to collide with. And also try OnTriggerEnter or OnTriggerStay and change collisions to collider and check isTrigger who hold this script.

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 SpaceManDan · May 24, 2017 at 09:34 PM

Lets back up a second. Break it down to the bare necessities first. Set aside the pickaxe you are using and do the following.

Pickaxe Design

Make GameObject -> 3D Object -> Cube

Name = Pickaxe Tag = Pickaxe Layer = Default

Inspect the cube in the inspector and find the Collider component. 1. Make sure it is enabled. 2. Make sure IsTrigger is disabled. by default this is how it should be set up, but just check just in case.

Add: Rigidbody 1. Disabled Gravity 2. Eneable Kinematic.

Now, move the gameobject in the hierarchy so that it is a child of the VR controller. If you are using the Vive it will be like so [CameraRig] -> ViveController(left or right) -> Pickaxe. Oculus is pretty much exactly the same.

Pickaxe is ready. It will give physics data to fixedUpdate now and receive it's movement data from the vive controller. i.e. It will follow the controller and tell the game engine it is smacking stuff. What you do with the smacks is what comes next.

MinedObject Design

Create GameObject -> 3D Object -> Cube Name = MineableBlock Tag = Block Layer = Default

Check the collider in the inspector. A. Enabled? B. IsTrigger = false

Add Rigidbody 1. Disable Gravity 2. Set IsKinematic to true This will cause the game object to float in one place without being able to be moved by the physics engine however it will still exists to the physics engine every fixedUpdate. I.E. It can be hit.

Now create a C#Script

Call it Block Put it on the Block (Dont forget to do this :D) Open the script for editing and add the following.

 private void OnCollisionEnter(Collision collision)
 {
     if (collision.collider.tag == "Pickaxe")
     {
         Debug.Log("OW!");
     }
 }

Save the script. Hit play, and whack the block. Check the console for "OW!"

NOTE that we used, Collision.collider.tag, not Collision.gameobject.tag

If you add this instead, you will see why we do that.

 private void OnCollisionEnter(Collision collision)
 {
     Debug.Log(collision.gameobject.tag);
     Debug.Log(collision.collider.tag);
 }

WE DID IT YAY!

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 mitch1gibson · May 25, 2017 at 01:42 PM 0
Share

Thanks but unfortunately this hasn't worked it's so strange.

avatar image SpaceManDan mitch1gibson · May 25, 2017 at 01:49 PM 0
Share

That is weird. Works on my end. $$anonymous$$akes me wonder, make sure the tag is spelled exactly like the tag on the gameobject including caps. $$anonymous$$ake sure the script is on the right object.

If it still doesn't work, make a screen cap of the pickaxe selected in the hierarchy so I can see it's setup in the inspector and also the block.

Screencap for windows = CTRL + SHIFT + PrtScr Open Paint CTRL + V to paste screen image

avatar image mitch1gibson SpaceManDan · May 26, 2017 at 04:02 PM 0
Share

alt text

unity.png (479.0 kB)

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

380 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 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 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 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 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 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 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 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

GameObject not colliding properly with a collider! 3 Answers

Ignore collision based on position 1 Answer

Collision With Text 0 Answers

How To how ?? 2 Answers

Collision or Triggers both not working. 3D. 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