- Home /
brick does not detect collision although ball does
okay so the ball can detect collision easily, but the brick its colliding with does not, now i know i could work with just writing the code to destroy the brick inside the sphere's script but i am stubborn and i really wanna know as to why my brick wont detect whats colliding into it. please help me i am just learning and starting off in unity and programming entirely. I really cant wrap my mind around this issue. thank you for the support thumbs up
btw both objects have a rigidbody and collider
code below;
[code=Csharp]
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class BrickCollison : MonoBehaviour { public int brickhealth; public int cur_brickH;
void Start ()
{
brickhealth = 100;
cur_brickH = brickhealth;
}
// Update is called once per frame
void Update ()
{
if (cur_brickH <= 0)
{
Destroy (gameObject);
}
}
void OnCollisonEnter(Collision brickcol)
{
if(brickcol.rigidbody.name == "Ball")
{
print ("Hit");
cur_brickH -= 50;
}
}
} [Code/]
I'm a noob to so bare with me, but if this script is attached to the brick and you call if (brickcol.rigidbody.name == "Ball")
then the brick is detecting that the "Ball" is hitting the Brick. This script takes -50 health from itself and has a Destroy (gameObject) when the health is <=0. If the "Ball" hits this Brick two times it should destroy itself. Is the script not working for you? Exta note: if this is all the script is going to do and nothing else will be added to it. Having both [brickhealth, cur_brickH] is unnecessary. brickhealth = -50; ,and if (brickhealth <= 0) should work just fine.
Answer by JaredHD · Apr 17, 2017 at 09:46 AM
Create a script on the Brick called "BrickScript", and make sure the brick has 2 colliders. One that has "Is Trigger" enabled and is slightly larger than the brick, and the other that is normal sized without "Is Trigger", then add the following code:
using UnityEngine;
public class BrickScript : MonoBehaviour
{
public float health = 100;
private void OnTriggerEnter(Collider other)
{
if (other.name == "Ball" || other.gameObject.tag == "Player")
{
Debug.Log("Hit");
health -= 50;
}
}
}
That will damage the brick. Afterwards use Update to check if health
@$$anonymous$$idroflz The best way would be use this ^^^ script and set your ball with the tag "Player" https://docs.unity3d.com/ScriptReference/GameObject-tag.html
This way if you clone the "Ball" gameobject ("BallClone"), or if it has a name other than "Ball" for any reason the collision will still be detected.
i was thinking of doing that not, with the same script but i am just curious as to why the box itself is not detecting that it is being hit, iv had to write all the code literally in the ball because it is the only thing that is detecting collision. :\ its more of a why is this brick being weird ?, why can it not detect being hit ? why does the the sphere detect that it is hitting the brick ?
Answer by PixelSpartan · Apr 17, 2017 at 09:55 AM
You have to have a rigidbody (even kinematic) to detect collisions
both objects have a rigid body. my problem is that the ball will detect collision when colliding with the box but the box doesn't seem to notice it is being hit
In brick collision script try using .transform.name or .transform.tag ins$$anonymous$$d of .rigidbody If not working then make the oncollision print the .transform.name
You can see in the other guy's trigger solution he uses just .name or .gameObject.tag
true, ive used gameobject but not transform. thank you :] ill give it a go
Your answer
Follow this Question
Related Questions
Avoid Player bouncing when colliding with objects 3D 1 Answer
Colliders not working when rotating 2 Answers
How to set collision for an object with specific collider size? 3 Answers
Collider + multiple sounds script 1 Answer
Why the npc character walking strange when using a Rigidbody and Is Kinematic on enabled ? 1 Answer