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 Lypheus · Apr 29, 2011 at 02:41 AM · colliderplaneheight

Determine Distance to Top of Collider?

I have some code where my character can "climb up" onto an object in front of them. Right now, I'm using a RaycastHit to determine if a climbable object exists in front of them and that part works great. Now I want to be able to detect the height of the collider plane in front of the player. I'm attempting this with the "mpCenter" vector and using a Bounds.Contains with marginal success.

What I'd really like is to know exactly what the world coordinate is of the tallest point of the colliders surface for my initial RaycastHit. That way, if it is a box collider for a wall for example, then i want the height of the plane (normal?) in front of the player - which will allow me to adjust my animation accordingly (i.e. if its about eye level then just play the climb up on ledge animation, if its a few feet above them, then play a jump animation first to get the player to the right height, then play climb up on ledge).

RaycastHit hit; if( Physics.Raycast( transform.position, transform.forward, out hit, 0.35f ) ) { // check if this is a climbable object or we are free to jump if( hit.transform.gameObject.tag == "Climbable" ) { Vector3 mpCenter = new Vector3( hit.point.x, rigidbody.position.y + jumpHeight, hit.point.z ); if( !hit.collider.bounds.Contains( mpCenter ) ) { animation.CrossFade( "ClimbUpOnLedge" ); } } }

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
5
Best Answer

Answer by Kourosh · Apr 29, 2011 at 03:45 AM

You can get the size of your wall (with a box collider) this way:

var length:float = hit.transform.localScale.x * hit.collider.size.x;
var width:float = hit.transform.localScale.z * hit.collider.size.z;
var height:float = hit.transform.localScale.y * hit.collider.size.y;
var dimensions:Vector3 = Vector3(length, height, width); 

now to know the world position of top most level of the wall:

var topMost:float = hit.transform.position.y + dimensions.y/2;
Comment
Add comment · Show 3 · 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 Lypheus · Apr 29, 2011 at 06:38 AM 0
Share

Works great, thanks :)!

avatar image swisscoder · Sep 15, 2011 at 12:30 PM 1
Share

In c#, you need to cast accordingly (put it here, that others can copy-paste without having to convert manually):

         float length = hit.transform.localScale.x * ((BoxCollider)hit.collider).size.x;
         float width = hit.transform.localScale.z * ((BoxCollider)hit.collider).size.z;
         float height = hit.transform.localScale.y * ((BoxCollider)hit.collider).size.y;
         Vector3 dimensions = new Vector3(length, height, width);

         //now to know the world position of top most level of the wall:
         float top$$anonymous$$ost = hit.transform.position.y + dimensions.y / 2;
avatar image Dsiak · Aug 09, 2018 at 03:58 AM 0
Share

I'll add a comment as other people will find this though google as I did and it's relevant.

Both codes are not taking into account the offset of the Box Collider, and will be inaccurate if you have it, the error is enormous if you are using big scales. To account for it just add the offset to the calculations.

 float height = hit.transform.localScale.y * ((BoxCollider)hit.collider).size.y + (hit.transform.localScale.y * ((BoxCollider)hit.collider).offset.y)



avatar image
0

Answer by iuripujol · Apr 23, 2016 at 10:50 AM

hit.collider.GetComponent ().bounds.center.y + 0.97f * hit.collider.GetComponent ().bounds.extents.y + 0.075f;//needs to match all!

Combine this with this and it will give you awesome results.

transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation (hit.normal * -1), Time.deltaTime); if (m_Animator.GetCurrentAnimatorStateInfo (0).IsName ("Climb")) { m_Controller.enabled = false; }

m_Animator.MatchTarget (m_Target, new Quaternion (), AvatarTarget.LeftHand, new MatchTargetWeightMask (new Vector3(0,m_Target.y,0), 0), ClimbMatchTargetStart, ClimbMatchTargetStop);

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 iuripujol · Apr 23, 2016 at 11:07 AM 0
Share

if 0.97f or 0.0075f do not match, change'em and match'em with new values, hope it helps...

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

2d plane falls through and half disapears when on terrain 0 Answers

How to hit two object with one raycast? 2 Answers

Change unity's first person controller collider size 2 Answers

Default plane collider doesnt work properly when stretched? 0 Answers

How can I use raycasthit and collider raycast to hit only if the mouse position is over a specific object ? 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