Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Kidroflz · Apr 16, 2017 at 01:39 PM · collisioncolliderscripting beginnercollision detection

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/]

Comment
Add comment · Show 1
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 bleachdrinker · Apr 17, 2017 at 08:29 AM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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

Comment
Add comment · Show 2 · 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 bleachdrinker · Apr 17, 2017 at 07:29 PM 0
Share

@$$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.

avatar image Kidroflz · Apr 20, 2017 at 12:14 AM 0
Share

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 ?

avatar image
0

Answer by PixelSpartan · Apr 17, 2017 at 09:55 AM

You have to have a rigidbody (even kinematic) to detect collisions

Comment
Add comment · Show 4 · 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 Kidroflz · Apr 20, 2017 at 12:11 AM 0
Share

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

avatar image PixelSpartan Kidroflz · Apr 20, 2017 at 12:23 AM 0
Share

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

avatar image PixelSpartan PixelSpartan · Apr 20, 2017 at 12:24 AM 0
Share

You can see in the other guy's trigger solution he uses just .name or .gameObject.tag

avatar image Kidroflz · Apr 20, 2017 at 12:32 AM 0
Share

true, ive used gameobject but not transform. thank you :] ill give it a go

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

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


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