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 unity_Vpi53N_fb99F6g · Jan 23, 2020 at 06:59 PM · collisiontrigger

On trigger enter sometimes not working

I'm trying to make a character controller (which can wall run). I currently have unity's character controller, with a slightly thicker capsulle collider around it set to trigger, to detect collisions alt text

However, sometimes when I jump off a wall, and hit another, the collider doesn't detect this collision (it tends to happen when I'm moving). I have a video demonstrating it: https://www.youtube.com/watch?v=dWfHc8govX4

Colliding code:

 private void OnTriggerEnter(Collider other)
     {
         //If the player enters collison with something other than themselves, set colliding to true
         if(other.gameObject != gameObject)
         {
             colliding = true;
         }
     }

     private void OnTriggerExit(Collider other)
     {
         //If the player exits collison with something other than themselves, set colliding to false
         if (other.gameObject != gameObject)
         {
             colliding = false;
         }
 
         //If detached from a wall, set the normal to 0, to avoid bugs
         if (other.gameObject.tag.Equals("Wall Run"))
         {
             wallRunNormal = Vector3.zero;
         }
     }

Wall run code:

 void WallRun()
     {
         //Wall running is true if it is a vertical wall, if the player is not touching the floor and if the player is colliding with something
         wallRunning = Vector3.Angle(Vector3.up, wallRunNormal) > 89 && !player.isGrounded && colliding;
 
         if (wallRunning)
         {
             //Sets 'targetRot' to the camera's rotation to better modify it
             Vector3 targetRot = camera.transform.localEulerAngles;
 
             //Depending on the wall surface the z rotation will be set taking into account the axis the normal of the surface is pointing
             if ((int)wallRunNormal.x != 0)
                 targetRot.z = wallRunRotation * -wallRunNormal.x * transform.forward.z;
             else if ((int)wallRunNormal.z != 0)
                 targetRot.z = wallRunRotation * wallRunNormal.z * transform.forward.x;
 
             //Lerps the rotation of the camera
             camera.transform.localRotation = Quaternion.Lerp(camera.transform.localRotation, Quaternion.Euler(targetRot), Time.deltaTime * wallRunRotationSmooth);
 
             //Sticks the player to the surface of the wall by "atracting" it (moving the player in the oposite direction of the wall normal)
             finalMovement.x -= wallRunNormal.x * speed * 2 * Mathf.Abs(transform.forward.z);
             finalMovement.z -= wallRunNormal.z * speed * 2  * Mathf.Abs(transform.forward.x);
 
             //When the player sticks to the wall, they do a little jump
             if (!done)
             {
                 yVel = jumpForce;
                 done = true;
             }
             
         }
         else
         {
             //Lerps the camera back to zero when not wall running
             Vector3 targetRot = camera.transform.localEulerAngles;
             targetRot.z = 0;
             targetRot.y = 0;
             camera.transform.localRotation = Quaternion.Lerp(camera.transform.localRotation, Quaternion.Euler(targetRot), Time.deltaTime * wallRunRotationSmooth);
 
         }
 
         //Detaches the player from the wall if they jump 
         if (wallRunning && Input.GetButton("Jump"))
         {
             AddForce(new Vector3(wallRunNormal.x * wallImpulse, jumpForce, wallRunNormal.z * wallImpulse));
             colliding = false;
             return;
         }
 
         if (!wallRunning)
         {
             done = false;
         }
             
 
         //Update the player movement
         MovePlayer();
     }

Force code (When the player jumps off a wall, force is added)

 void Force()
     {
         if(force.y != 0)
         {
             jumping = true;
             yVel = force.y;
             force.y = 0;
         }
 
         if (!player.isGrounded || crouching)
         {
             force = Vector3.Lerp(force, Vector3.zero, 1f * Time.deltaTime);
         }
         else
         {
             force = Vector3.Lerp(force, Vector3.zero, 7f * Time.deltaTime);
         }
         
         finalMovement += force;
     }

Movement script

 void Movement()
     {
         //Gets the input
         float hMovement = Input.GetAxis("Horizontal");
         float vMovement = Input.GetAxis("Vertical");
 
         //If the player can sprint, when they hold the sprint key, lerp to the sprint speed, when they let go, lerp to the original speed
         if (canSprint)
         {
             if (Input.GetButton("Sprint"))
             {
                 speed = Mathf.Lerp(speed, sprintSpeed, Time.deltaTime * sprintSmooth);
             }
             else if (!Input.GetButton("Sprint"))
             {
                 speed = Mathf.Lerp(speed, prevSpeed, Time.deltaTime * sprintSmooth);
             }
         }
 
         
 
         //Translates the input into something we can use
         Vector3 zMovement = transform.forward * vMovement;
         Vector3 xMovement = transform.right * hMovement;
 
         //Stores the final movement so it can be clamped
         movement = xMovement + zMovement;
 
         //Clamps the movement
         movement = Vector3.ClampMagnitude(movement, 1);
 
         //Ads some resistance and momentum in the air
         if (player.isGrounded || wallRunning)
         {
             finalMovement = new Vector3(movement.x * speed, yVel, movement.z * speed);
             groundedMovement = finalMovement;
             groundedMovement.y = 0;
         }
 
         if (!player.isGrounded && !wallRunning)
         {
             finalMovement = groundedMovement;
             groundedMovement += movement;
             groundedMovement = Vector3.ClampMagnitude(groundedMovement, speed * 2);
             groundedMovement = Vector3.Lerp(groundedMovement, Vector3.zero, 1f * Time.deltaTime);
             finalMovement.y = yVel;
         }
 
         //Applies the movement
         if (!crouching)
             MovePlayer();
     }

That's all I can think of that would affect this. Thanks in advance.

unity-2019114f1-personal-samplesceneunity-some-gam.png (45.0 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

4 Replies

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

Answer by unity_Vpi53N_fb99F6g · Jan 26, 2020 at 04:40 PM

I'm just going to redo the script with a rigidbody because the character controller is giving me a lot of problems

Comment
Add comment · 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
1

Answer by lgarczyn · Jan 24, 2020 at 10:23 PM

A few things are weird.


  1. I don't think a rigidbody can ever collide with themselves.

  2. Since you can collide with multiple colliders at once, you need a collide counter, not a colliding Boolean. This is likely your problem

  3. Your code has a lot of side-effects (here, you are using a lot of private variables instead of local), which would make it hard to debug

  4. You haven't posted the code for MovePlayer, but if it uses the transform instead of CharacterController.Move(), you might miss collision events

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 unity_Vpi53N_fb99F6g · Jan 25, 2020 at 06:22 PM 0
Share

The thing is, since the collider I'm using to detect collisions is slightly thicker than the player controller, it collides with it. I've tried the whole counter thing, and it doesn't work, but that's not the problem, the problem is that when the player jumps off a wall and hits another (while moving forward), this collision is not detected by the collider. And as for movement, I'll post the code, but it's really simple and it does use CharacterController.$$anonymous$$ove(). $$anonymous$$y best guess is that the problem has to do with how i tackled momentum and force, but I'm out of ideas. Thank you so much for the response though.

avatar image lgarczyn unity_Vpi53N_fb99F6g · Jan 25, 2020 at 06:37 PM 0
Share

Are you using both a character controller and a rigidbody? Damn, that will definitely not work.

Use OnControllerColliderHit. And use a counter.

avatar image unity_Vpi53N_fb99F6g lgarczyn · Jan 25, 2020 at 07:08 PM 0
Share

I am, but it is set on kinematic and it's just so the collider attached to the player object detects collisions. If I use OnControllerColliderHit, how will I know if the player has exited a collision? Because I've done some research and Unity doesn't provide any information for that I believe.

Show more comments
avatar image
0

Answer by logicandchaos · Jan 25, 2020 at 01:32 AM

Have you also checked that colliding = false before the 2nd collision?

Comment
Add comment · Show 1 · 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 unity_Vpi53N_fb99F6g · Jan 25, 2020 at 06:28 PM 0
Share

Yes, whenever the player jumps off a wall, colliding is set to false. The problem is that when a player hits anotehr wall, this collision isn't detected

avatar image
0

Answer by Devivk · Nov 16, 2021 at 10:26 AM

I've been dealing with this issue for some time as well, it's a fairly annoying thing to deal with but I finally found a workaround I believe.

As most of us know when the Char Controller hits the collider it will ignore the collision and it turns out instead of colliding with the entity involved it will stand on top of it if able (maybe be pushed away from it if possible i'm unsure haven't tested it out as a wall). Well by increasing the values of the colliders Scale so the player has no choice but to walk through it you won't be able to ever "Stand" on the trigger, you'll always be forced to walk through it and activate trigger effects. My jump pad wasn't working previously and by doing this it works every time now.

Also for anyone interested in an easy way to get collisions working with this method you can follow something like this

 private void OnTriggerEnter(Collider other)
     {
         Debug.Log("Collision");
         // One way to do this better, might be to check componenets on the object.            
         // if the player collides with the object
         if (other.gameObject.layer == 10) { Debug.Log("Player"); PlayerCollision(other.gameObject); }
     }
 
     private void PlayerCollision(GameObject _player)
     {
         _player.GetComponent<FirstPersonPlayerController>().addForce(direction);
     }
Comment
Add comment · 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

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

196 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

Related Questions

Activate trigger if items colected 1 Answer

Play Animation OnTriggerEnter 2 Answers

Targeting the GameObject I collide with? 1 Answer

Whats wrong with my score script 1 Answer

Trigger Spawning? 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