Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 boddole · Feb 11, 2015 at 04:58 AM · c#colliderontriggerenterproceduralontriggerexit

Strange OnTriggerEnter/Exit() Behavior

Hello everyone, I'm completely stumped as to what is going on with OnTriggerEnter() and OnTriggerExit().

My Issue: What I'm getting is that when an object enters my collider, both OnEnter() and OnExit get called, then when the collider leaves the mesh, once again Enter() and Exit() get called.

In the attached photo, the large purple cube is the moving collider, and the mesh that is checking for movement is the green outlined mesh. When the Cube enters Enter() is called, once completely inside the collider Exit() is called, then when exiting the mesh, Enter() is called, then once the mesh has completely exited the mesh Exit() is called.

I would have to guess that it has to do with my mesh that has a Mesh Collider on it (which is created proceduraly), but I can't figure out what that would be (and I have included the script used to make that procedural mesh)...

Any ideas as to what is going wrong is appreciated, thank you for reading.

alt text

 using UnityEngine;
 using System.Collections;
 using System.Linq;
 
 public class Ship_GunFOVBuilder: MonoBehaviour {
     //responisble for creating the mesh the gun will use to determine what targets it cares about
     //placed on some EMPTY??? base hierarchy object
     //10 16 2014, this script has been combined with another scipt found on Unity Wiki for making procedural cubes...
 
     //
     #region Debugging:
     [Header("Debugging:")]
     public Material mat1; //debugging, used to render object.
     #endregion Debugging:
     //
     //
     #region CalculateNextVertice():
     private Ray tempRay = new Ray();
 
     private Vector3 leftRightVec = Vector3.zero;
     private Vector3 upDownVec = Vector3.zero;
 
     private Quaternion leftRight = Quaternion.identity;
     private Quaternion upDown = Quaternion.identity;
     private Quaternion comb = Quaternion.identity;
 
     private Vector3 normalized = Vector3.zero;
     private Vector3 createdVec3 = Vector3.zero;
 
     private float trueDistance;
     #endregion CalculateNextVertice():
     //
     private Ship_GunGroup gunGroup; //need to take some values for each gun from the gun group
     private GameObject gunFOVGO; //child GO on which the components are added to.
 
 
     public void InitialSetup()
     {
         gunGroup = gameObject.GetComponentInParent<Ship_GunGroup>();
         gunFOVGO = transform.GetChild(0).gameObject;
         gunFOVGO.layer = 2; //ignore raycast layer.
         //Debug.Log("gunFOVGO.name is: " + gunFOVGO.name);
         
         MeshFilter meshFilter = gunFOVGO.AddComponent<MeshFilter>();
         Mesh created = MeshBuilder();
         meshFilter.mesh = created;
         
         MeshRenderer meshRenderer = gunFOVGO.AddComponent<MeshRenderer>();
         meshRenderer.material = mat1;
         meshRenderer.enabled = true; //as long as it works, it does not need to be drawn (collider box will show its position).
         
         //for actual deployment:
         meshRenderer.enabled = false; //as long as it works, it does not need to be drawn (collider box will show its position).
         //
 
         MeshCollider meshCollider = gunFOVGO.AddComponent<MeshCollider>();
         meshCollider.isTrigger = true;
         
         Rigidbody rigidbody = gunFOVGO.AddComponent<Rigidbody>();
         rigidbody.isKinematic = true;
         rigidbody.useGravity = false;
     }
     
     public void RecurringSetup()
     {
         //
     }
     
 
     private Mesh MeshBuilder()
     {
         Mesh newMesh = new Mesh();
         newMesh.name = "FOV mesh";
         newMesh.Clear();
         
         //float length = 1f;
         //float width = 1f;
         //float height = 1f;
         
         #region Vertices
         //FRONT ->  input from gunGroup
         //top left
         //Vector3 p0 = new Vector3( -length * .5f,    -width * .5f, height * .5f );
         Vector3 p0 = gunGroup.fovTopLeftBase;
         
         //top right
         //Vector3 p1 = new Vector3( length * .5f,     -width * .5f, height * .5f );
         Vector3 p1 = gunGroup.fovTopRightBase;
         
         //bottom right
         //Vector3 p2 = new Vector3( length * .5f,     -width * .5f, -height * .5f );
         Vector3 p2 = gunGroup.fovBotRightBase;
         
         //bottom left
         //Vector3 p3 = new Vector3( -length * .5f,    -width * .5f, -height * .5f );
         Vector3 p3 = gunGroup.fovBotLeftBase;
         
         //BACK -> calculated(not sure if should look at back from through front, or only back...So I'm not sure how to name these points...
         //top left
         //Vector3 p4 = new Vector3( -length * .5f,    width * .5f,  height * .5f );
         Vector3 p4 = CalculateNextVertice(p0, gunGroup.fovUpAngle, gunGroup.fovLeftAngle, gunGroup.fovDistance);
         
         //top right
         //Vector3 p5 = new Vector3( length * .5f,     width * .5f,  height * .5f );
         Vector3 p5 = CalculateNextVertice(p1, gunGroup.fovUpAngle, gunGroup.fovRightAngle, gunGroup.fovDistance);
         
         //bottom right
         //Vector3 p6 = new Vector3( length * .5f,     width * .5f,  -height * .5f );
         Vector3 p6 = CalculateNextVertice(p2, gunGroup.fovDownAngle, gunGroup.fovRightAngle, gunGroup.fovDistance);
         
         //bottom left
         //Vector3 p7 = new Vector3( -length * .5f,    width * .5f,  -height * .5f );
         Vector3 p7 = CalculateNextVertice(p3, gunGroup.fovDownAngle, gunGroup.fovLeftAngle, gunGroup.fovDistance);
         
         
         Vector3[] vertices = new Vector3[]
         {
             // Bottom
             p0, p1, p2, p3,
             
             // Left
             p7, p4, p0, p3,
             
             // Front
             p4, p5, p1, p0,
             
             // Back
             p6, p7, p3, p2,
             
             // Right
             p5, p6, p2, p1,
             
             // Top
             p7, p6, p5, p4
         };
         #endregion
         
         #region Normales
         Vector3 up     = Vector3.up;
         Vector3 down     = Vector3.down;
         Vector3 front     = Vector3.forward;
         Vector3 back     = Vector3.back;
         Vector3 left     = Vector3.left;
         Vector3 right     = Vector3.right;
         
         Vector3[] normales = new Vector3[]
         {
             // Bottom
             down, down, down, down,
             
             // Left
             left, left, left, left,
             
             // Front
             front, front, front, front,
             
             // Back
             back, back, back, back,
             
             // Right
             right, right, right, right,
             
             // Top
             up, up, up, up
         };
         #endregion    
         
         #region UVs
         Vector2 _00 = new Vector2( 0f, 0f );
         Vector2 _10 = new Vector2( 1f, 0f );
         Vector2 _01 = new Vector2( 0f, 1f );
         Vector2 _11 = new Vector2( 1f, 1f );
         
         Vector2[] uvs = new Vector2[]
         {
             // Bottom
             _11, _01, _00, _10,
             
             // Left
             _11, _01, _00, _10,
             
             // Front
             _11, _01, _00, _10,
             
             // Back
             _11, _01, _00, _10,
             
             // Right
             _11, _01, _00, _10,
             
             // Top
             _11, _01, _00, _10,
         };
         #endregion
         
         #region Triangles
         int[] triangles = new int[]
         {
             // Bottom
             3, 1, 0,
             3, 2, 1,            
             
             // Left
             3 + 4 * 1, 1 + 4 * 1, 0 + 4 * 1,
             3 + 4 * 1, 2 + 4 * 1, 1 + 4 * 1,
             
             // Front
             3 + 4 * 2, 1 + 4 * 2, 0 + 4 * 2,
             3 + 4 * 2, 2 + 4 * 2, 1 + 4 * 2,
             
             // Back
             3 + 4 * 3, 1 + 4 * 3, 0 + 4 * 3,
             3 + 4 * 3, 2 + 4 * 3, 1 + 4 * 3,
             
             // Right
             3 + 4 * 4, 1 + 4 * 4, 0 + 4 * 4,
             3 + 4 * 4, 2 + 4 * 4, 1 + 4 * 4,
             
             // Top
             3 + 4 * 5, 1 + 4 * 5, 0 + 4 * 5,
             3 + 4 * 5, 2 + 4 * 5, 1 + 4 * 5,
             
         };
         #endregion
         
         newMesh.vertices = vertices;
         
         newMesh.normals = normales;
         
         newMesh.uv = uvs;
         
         newMesh.triangles = triangles;
         //reverse them to invert normals (which fixes an issue where all normals are inverted for an unkown reason)...
         newMesh.triangles = newMesh.triangles.Reverse().ToArray();
         
         newMesh.RecalculateBounds();
         
         newMesh.Optimize();
         
         return newMesh;
     }
     
     //using parameters, calculate where the desired point will be:
     private Vector3 CalculateNextVertice(Vector3 startingPoint, float desiredUpDownAngle,  float desiredLeftRightAngle, float desiredDistance)
     {
         //new way, make normalized vector and feed those together to make final angle (desired angle / 90 since that is the max possible angle
         tempRay.origin = startingPoint;
 
         leftRightVec.x = 0f;
         leftRightVec.y = desiredLeftRightAngle;
         leftRightVec.z = 0f;
 
         upDownVec.x = desiredUpDownAngle;
         upDownVec.y = 0f;
         upDownVec.z = 0f;
 
         leftRight = Quaternion.Euler(leftRightVec);
         upDown = Quaternion.Euler(upDownVec);
         comb = leftRight * upDown;
         normalized = comb * Vector3.forward;
         tempRay.direction = normalized;
 
         //calc desired distance:
         trueDistance = (desiredDistance / Mathf.Cos(desiredUpDownAngle * Mathf.Deg2Rad) );
         //Debug.Log("desiredUpDownAngle is: " + desiredUpDownAngle);
         //Debug.Log("(desiredDistance / Mathf.Cos(desiredUpDownAngle * Mathf.Deg2Rad) ) is: " + (desiredDistance / Mathf.Cos(desiredUpDownAngle * Mathf.Deg2Rad) ) );
         //Debug.Log("trueDistance is now: " + trueDistance);
         //Debug.Log("trueDistance is now: " + trueDistance);
 
         Physics.Raycast (tempRay.origin, tempRay.direction, trueDistance);
         //Debug.DrawRay (tempRay.origin, tempRay.direction * trueDistance, Color.red, 5f);
 
         //get the point at the end of the ray:
         createdVec3 = tempRay.GetPoint (trueDistance);
         
         //Debug.Log ("createdVec3 is: " + createdVec3);
         return createdVec3;
     }
 }

example-a.png (190.3 kB)
Comment
Add comment
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

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

19 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

Related Questions

issue with making a moving object trap c# (SOLVED) 2 Answers

Slenderman like game pages not working! 2 Answers

I'm trying to adjust tagged objects meshrenderer in C# code. 1 Answer

Multiple Cars not working 1 Answer

Collision problem in C# 4 Answers


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