Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 gouzarte · Jun 30, 2021 at 02:55 PM · collisiontriggerplayeroncollisionenterkill player

killzone not detecting player

I want an invisible cube that kills the player if the player touches it. the player runs on a player controller, using a script called walk.cs, has 3f health and the process used to lower said health is ddamage

I set the cube so it has no renderer, and added the trigger tag to the cube collider, but its not even printing "pão" like I asked it to, which means it isnt even detecting the collision

also, I have tried using OnControllerColliderHit, but it didnt work either

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class killzone : MonoBehaviour
 {
     void OnCollisionEnter(Collision col)
     {
         if(col.gameObject.tag == ("Player"))
         {
             print("pão");
             walk target = col.transform.GetComponent<walk>();
             target.ddamage(3f);
         }
     }
 }
 
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 bdubbert · Jun 30, 2021 at 04:20 PM 0
Share

Try OnTriggerEnter rather than OnCollisionEnter

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by tyruji · Jun 30, 2021 at 06:01 PM

OnCollision, as well as OnTrigger messages, are called by game objects with a rigidbody attached, respectively OnControllerColliderHit is called for game objects with a character controller. Make sure you have either and you'll be good.

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 gouzarte · Jun 30, 2021 at 06:38 PM 0
Share

I tried using OnControllerColliderHit, since my player is based on a Character Controller, but no matter how many alterations I make, it doesnt seem to make a difference, and the player still isnt being detected.

 public class killzone : MonoBehaviour
 {
     void OnControllerColliderHit(ControllerColliderHit col)
     {
         if (col.gameObject.CompareTag("Player"))
         {
             print("pão");
             walk target = col.transform.GetComponent<walk>();
             target.ddamage(3f);
         }
     }
 }
avatar image tyruji · Jul 05, 2021 at 10:24 PM 0
Share

Aight, I'll repeat myself - OnControllerColliderHit will be called for game objects with a character controller attached. I assume your player has a character controller attached, your script will work if your killzone has a character controller on it as well. If your killzone is a cube or multiple cubes then you could use Physics.OverlapBox to get all colliders overlapping with your cubes. Something like that: private BoxCollider[] _boxColliders = null;

 private void Start()
 {
         _boxColliders = GetComponents<BoxCollider>();
 }
 private void Update()
 {
         Player player = null;
         foreach( var box in _boxColliders )
         {
                 foreach( var col in Physics.OverlapBox(transform.position +                 
                                                 box.center, box.size/2, transform.rotation) )
                 {
                         if( !col.gameObject.CompareTag("Player") ) return;
                         player = col.gameObject.GetComponent< Player >();
                 }
                 if( player != null ) break;
         }
         if( player == null ) return;
         player.Damage( 420 ); // do whatever you want
 }

Hope I helped somehow, good luck.

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

221 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

Related Questions

Tag Player in VRTK 1 Answer

2D Platformer - Picking Up Items & Storing Them C# 0 Answers

Colliding two GameObjects 1 Answer

How do i destroy only 1 object when 2 instances of the same object collide ? 2 Answers

Compound collider does not trigger the parent 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