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
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.
Thanks for the help, this doesnt seem to work perhaps its something to do with the pick on a vr controller?
it is important to note that the pick colliders also will need kinematic rigid bodies as well.
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.
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!
Thanks but unfortunately this hasn't worked it's so strange.
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
Your answer
Follow this Question
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