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 Aridez · May 09, 2014 at 07:10 PM · directionplanepointsvectors

Get gameobject limits

Im using a raycast to detect if a gameobject is between the target, what I want now is to get the distance between the point of the object hit and the left and right points where the object ends. I attach an image for a better understanding here. Thanks for the help and tell me if you can't understand the question (I dont know exactly how to explain it but I can attach a more precise screenshot).

Comment
Add comment · Show 4
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 Landern · May 09, 2014 at 07:12 PM 0
Share

Are you referring to the bounding box or the mesh and left and right points where the object ends?

avatar image robertbu · May 09, 2014 at 07:15 PM 0
Share

Do you know the size of the 'wall' with a localScale of (1,1,1). Is the 'wall' scaled? Are the arrows in your drawing always across a particular local axis (i.e. the 'x' axis of the object)?

avatar image Aridez · May 09, 2014 at 07:38 PM 0
Share

I tried using the bounding box but when I rotated the wall across the 'y' axis I didn't get the results I was expecting from getting the max and $$anonymous$$ bounds.

The arrows depend on the rotation of the object (I guess they can be on the x or z axis depending on its rotation).

avatar image Aridez · May 09, 2014 at 07:40 PM 0
Share

I forgot to say that what I want to get is the point at which the collider attached ends (like drawing a line perpendicular to a raycast recieved to the center of the collider until you reach the end of the box), thanks!

1 Reply

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

Answer by robertbu · May 09, 2014 at 08:37 PM

You can use local coordinates and mesh.bounds to find these edges. Here is an example script. Note it uses the alignment between the normal and the forward to detect whether you clicked on a back/front side or a left/right side. This will not work for an arbitrary shaped object...just cubes.

To test, start a new scene. And and resize a cube. Add the script to an empty game object. Click on the cube. It positions a couple of marker spheres on the edges and outputs in a Debug.Log(), the distance to the two edges:

 #pragma strict
 
 private var marker1 : Transform;
 private var marker2 : Transform;
 private var localXExtent : float;
 private var localYExtent : float;
 
 function Start() {
     marker1 = GameObject.CreatePrimitive(PrimitiveType.Sphere).transform;
     marker2 = GameObject.CreatePrimitive(PrimitiveType.Sphere).transform;
     marker1.localScale = new Vector3(0.15, 0.15, 0.15);
     marker2.localScale = new Vector3(0.15, 0.15, 0.15);
 }
 
 function Update() {
 
     if (Input.GetMouseButtonDown(0)) {
     var hit : RaycastHit;
     var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     
          if (Physics.Raycast(ray, hit)) {
             var target = hit.transform;    
             var extent : float;
             var mf = target.GetComponent(MeshFilter);
             var localHit = target.InverseTransformPoint(hit.point);
             if (mf != null) {
                 if (Mathf.Abs(Vector3.Dot(hit.normal, target.forward)) > 0.9) { 
                     extent = mf.mesh.bounds.extents.x;
                     marker1.position = target.TransformPoint(Vector3(-extent, localHit.y, localHit.z));
                     marker2.position = target.TransformPoint(Vector3(extent, localHit.y, localHit.z));
                 }
                 else {
                     extent = mf.mesh.bounds.extents.z;
                     marker1.position = target.TransformPoint(Vector3(localHit.x, localHit.y, -extent));
                     marker2.position = target.TransformPoint(Vector3(localHit.x, localHit.y, extent));            
                 }
                 Debug.Log("Distance 1: "+Vector3.Distance(marker1.position, hit.point));
                 Debug.Log("Distance 2: "+Vector3.Distance(marker2.position, hit.point));
             }
         }
     } 
 }
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 Aridez · May 09, 2014 at 10:14 PM 0
Share

Works almost perfectly!, I found some issues with the script when the hit point was on edges, but thank you a lot!

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

21 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

Related Questions

Plane geometry 0 Answers

Direction of a moving object 1 Answer

Get mapping postion of 1 point on a plane defined by 3 points 1 Answer

Tranposing two 3D coordinates onto a 2D plane? 1 Answer

How to find acute and obtuse angle between 2 points 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