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 Zayer · Jun 16, 2013 at 11:51 PM · collisioncolliderplayer2d-platformer

How to detect a collision between the character and the bottom of a cube?

Hi,

I'm new to Unity and right now I'm using a capsule with First Person Controller.

I put a foating block and my script detect when the player touch the side but not the top nor the bottom of the block.

This script is attached to the FPController (C#):

 using UnityEngine;
 using System.Collections;
 
 public class BlockInteraction : MonoBehaviour {
     public float pushPower = 2.0F;
     
     void OnControllerColliderHit(ControllerColliderHit hit) {
         
         Rigidbody body = hit.collider.attachedRigidbody;
         if (body == null || body.isKinematic)
             return;
         
         if (hit.moveDirection.y < -0.3F)
             return;
         
         Debug.Log("Collided");
     }
 
 }

(Used from the docs on the Unity website);

FPControler Properties: Transform > Scale: 1, 0.5, 1 Character Controller > Radius: 0.6 Character Controller > Height: 2

Graphics Properties: Scale: 0.4, 0.5, 0.4

I've tried adding a sphere on the top of the character, tried to make the radius higher for the capsule (Someone on the internet suggested that it might resole the problem, with no success)

Any help is appreciate.

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

2 Replies

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

Answer by robertbu · Jun 17, 2013 at 12:00 AM

You can check the CharacterController.collisionFlags to see if you got hit from above. Or you can check the normal in the ControllerColliderHit parameter of OnControllerColliderHit() function to see if it is facing downward. You can use either Vector3.Dot() or Vector3.Angle() to compare the normal to the downward direction. Or I've seen it done by adding empty child object to the cube (one for each side), each with its own collider.

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
0

Answer by Zayer · Jun 24, 2013 at 06:28 AM

Thanks for your answer. It helped me out a lot. I've looked on the Docs of Unity to learn more about Collider, Trigger, etc and I came up with my solution.

 void OnControllerColliderHit(ControllerColliderHit hit) {
         
         
         if ((hit.gameObject.tag == "SurpriseBlock") && (hit.moveDirection == new Vector3(0.0F,1.0F,0.0F) )) {
             //Hit from bottom
             hit.collider.renderer.material.color = Color.red;
 
         }
     }
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

14 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

Related Questions

Play annimation on collision not working. 1 Answer

Player detecting collision when crouching 1 Answer

Trigger help! 0 Answers

Multiplayer Layers and Colliders confusion. 1 Answer

Unity2D side collision detection 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