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 /
avatar image
0
Question by freddyhard · Jan 05, 2017 at 10:26 AM · terraincolliderstrees

OnTriggerExit not called for Tree painted on Terrain

  public void OnTriggerExit(Collider other)
     {
         if (other.gameObject.layer == _layer)
         {
             Debug.Log("OnTriggerExit: " + other.gameObject.name);
             _blocked = false;
         }
     }
 
     public void OnTriggerEnter(Collider c)
     {
         if (((1 << c.gameObject.layer) & Blocking) != 0)
         {
             Debug.Log("OnTriggerEnter: " + c.gameObject.name);
             _blocked = true;
         }
     }

When the tank hits an object it Instantiates this test object and rotates it left and right per frame until the OnTriggerExit is called. I have 2 videos linked where it works and then doesn't. It works when i have a Tree Prefab placed on the Terrain from the Assets, but it does not work when the same Tree Prefab is painted onto the Terrain using the Terrain tools in the inspector.
Does work
Does not work
I have a prefab with a collider on it to test if an area is free. The code for the Methods are as simple as it gets. Even when the Debug lines are outside the if statement they are still not printed, so the methods are never called.
So what it seems is that the Collider on the Terrain Tree does not call the OnTriggerExit, but other prefabs manually placed are called. Has anyone see this before?
I am using Unity 5.4.2. I'm not sure what other information is needed, but i'll provide it.

Comment
Add comment · Show 6
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 wachief · Jan 05, 2017 at 04:46 PM 0
Share

more information could help, like what components are on the GameObjectswith the trigger? does it have a rigidbody? does the tree has a rigidbody and so on. also sharing more of the code could help too.

is the OnTriggerEnter called?

anyway i would make sure that the GameObjects are set right. in order to trigger OnTriggerExit you need at least one of the GameObjects to have a rigidbody. ( as you can see on this page)

avatar image freddyhard · Jan 05, 2017 at 06:14 PM 0
Share
 using System.Collections.Generic;
 using UnityEngine;
 
 public class $$anonymous$$4Obstaclerator : $$anonymous$$onoBehaviour
 {
     public bool UNRESOLVED { get; private set; }
     public int DegreeJump = 30;
     public Layer$$anonymous$$ask Blocking;
     private bool _blocked;
     private int _layer;
     private int[] _steps;
     private int _i;
 
     public void Awake()
     {
         _blocked = true;
         _steps = new int[360 / DegreeJump];
         var counter = 0;
 
         foreach (var num in nextAngle())
         {
             if (counter == _steps.Length)
                 break;
             _steps[counter++] = num;
         }
     }
 
     public void OnTriggerExit(Collider other)
     {
         if (other.gameObject.layer == _layer)
         {
             Debug.Log("OnTriggerExit: " + other.gameObject.name);
             _blocked = false;
         }
     }
 
     public void OnTriggerEnter(Collider c)
     {
         if (((1 << c.gameObject.layer) & Blocking) != 0)
         {
             Debug.Log("OnTriggerEnter: " + c.gameObject.name);
             _blocked = true;
         }
     }
     
     public void SetColliderLayer(int layer)
     {
         _layer = layer;
     }
 
     public Transform FoundTarget()
     {
         if (_blocked)
         {
             transform.parent.RotateAround(transform.parent.position, transform.up, getDegree());
             return null;
         }
         return transform;
     }
 
     private int getDegree()
     {
         if (_i < _steps.Length)
             return _steps[_i++];
         
         UNRESOLVED = true;
         return 0;
     }
 
     private IEnumerable<int> nextAngle()
     {
         var iRight = 1;
         var iLeft = -2;
         while (true)
         {
             yield return iRight * DegreeJump;
             yield return iLeft * DegreeJump;
             iRight += 2;
             iLeft -= 2;
         }
     }
 }

Here is the entire script of the gameObject that i want the OnTriggerExit/Enter to get called. This gameObject that is being rotated only has a box collider with the "Is Trigger" checked and this script attached. Okay i have a render mesh on it as well because i want to see what it is doing for now, hence the big green box in the videos. The Tree Prefab has a RigidBody component.
So it works as expected when i manually place a tree in the scene, but not when i use the same tree with the Terrain tool. The Terrain tool is doing something else that i am unaware of or treats collisions differently to the Prefab - i guess.

avatar image wachief freddyhard · Jan 09, 2017 at 05:32 PM 0
Share

from what i can see so far there is not easy way to do what you are trying to do. the trees on the terrain are not the same as other game objects. (as you can see here )

there could be other ways of knowing where the tree is. you can use the terrain data.

i am not 100% sure what you are trying to do with this script, but another thing that could work for you is to add a nav$$anonymous$$esh, then every time you collide with a tree you could get the closest point on the nav$$anonymous$$esh.

avatar image freddyhard · Jan 09, 2017 at 07:51 PM 0
Share

i notice that the collider returned from the collision was that of the terrain, not any individual tree. I cannot find it now, but someone recommended a mass placement tool, which seems to be my solution.
What i am trying to do is a simple navigation around a small obstacle that may or may not be there. I don't think the nav$$anonymous$$esh works for dynamic objects? There will be other tanks moving around, or not (knocked out). For larger objects i have a path with target points that will be used to negotiate it.
i am very new to Unity, so my approach will be more than likely wrong! Thanks for the help.

avatar image wachief freddyhard · Jan 09, 2017 at 07:57 PM 0
Share

Sounds like you should give navmesh a chance. check out the Nav$$anonymous$$esh Obstacle it could help you. Also there are a lot of pathfinding plugins in the asset store that could help you. Some of them are free.

avatar image freddyhard · Jan 10, 2017 at 02:17 PM 0
Share

nav$$anonymous$$esh looks like it has a place when building a game, but it doesn't suit me on this occasion. Anyway, onwards i go!

0 Replies

· Add your reply
  • Sort: 

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

Removing colliders from terrain trees 2 Answers

How can I have mass amount of tress without performance loss? 2 Answers

Scripts on Terrain Trees 2 Answers

How to disableTerrain Collider but keep the Tree Colliders? 1 Answer

Disable collider on only 1 tree type not all? 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