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
1
Question by Socapex · Dec 07, 2013 at 05:23 PM · boundssnappingobject follow path

Get ClosestPointOnBounds when inside bounds?

Hello, I've been banging my head on this for hours now. I'll explain the context first. I'm making a railroad tycoon prototype. When placing roads, they will follow the mouse using a raycast hits. I placed a hidden quad around a "building" box to detect if I'm close to a building.

I want to snap the road position to the exterior of the collider.bounds. The problem is when using ClosestPointOnBounds, it will return the current position if I'm inside the bounds (and I always will be).

Possible solutions:

1) Add some values to my hit.point so I'm always outside the bounds, but closest to the hit point… What could I use to make this accurate?

2) An unknown function???

3) Create my own ClosestPointOnBounds, which seems really complicated from what I read…

4) Profit?

Some of my code to give a better idea of what I'm doing:

     void noClick()
     {
         if (newRoadStack.Count != 0)
         {
             
             RaycastHit hit;
             Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit);
 //            GameObject thisRoad = newRoadStack.Peek().GetComponentInChildren<Collider>().gameObject;
 
             if (hit.collider.tag == "Terrain")
             {
                 //hit = newCastIgnoreRoads();
                 doFollowMouse(hit);
             }
 
             else if (hit.transform.gameObject.name == "SnapZone")
             {
                 //doFollowMouse(hit);
                 doSnapToBuilding(hit);
             }
         }
     }

     void doSnapToBuilding(RaycastHit hit)
     {
         hit.point = hit.collider.ClosestPointOnBounds(hit.point); // <--- PROBLEM IS HERE, returns the original hit point :(
         doFollowMouse(hit);
     }

     void doFollowMouse(RaycastHit hit)
     {
         newRoadStack.Peek().transform.LookAt(hit.point); // Look at mouse
         
         // Resize road et FUCK C# CEST CAVE
         Vector3 stupidCSharp = newRoadStack.Peek().transform.localScale;
         float tempDistance = Vector3.Distance(newRoadStack.Peek().transform.position, hit.point);
         
         // If the mouse is too close, clamp the distance to one and dont show cause its ugly
         if (tempDistance < invisibleDistance)
         {
             tempDistance = 1;
             newRoadStack.Peek().GetComponentInChildren<Renderer>().enabled = false; // Show object
         }
         else
             newRoadStack.Peek().GetComponentInChildren<Renderer>().enabled = true; // Show object
         
         stupidCSharp.z = tempDistance;
         newRoadStack.Peek().transform.localScale = stupidCSharp;
 
     }

Thanks for any help. If you have a better way of going around with this problem, please feel free to explain :)

Comment
Add comment · Show 1
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 Socapex · Dec 07, 2013 at 05:32 PM 0
Share

O$$anonymous$$, as I was writing this, inspiration kinda struck. Since my "building" cube is always inside the SnapZone collider and my SnapZone is it's child, I can use the building GetPointsOnBound. This effectively snaps to the building edges and moves with my mouse, hurray.

Now, all I would need to do is add to my point the distance along the cube normal to the SnapZone bound… Any ideas? Is this clear at all? :)

2 Replies

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

Answer by Socapex · Dec 07, 2013 at 06:44 PM

Got it! :D

So, after realizing I could get the point on the bounds if my inner cube (the building), the solution was quite simple. Since the closest point to my mouse on the building will always be orthogonal, I can find the direction by substracting the inner point to my mouse hit point. I normalize that to get the direction towards the outer snap bound.

I then find the difference in size of my 2 bounds (exterior - interior) and scale my direction accordingly. I can now add this to the inner point I found originally and TADA! Works :)

Here is the relevant part of the code:

     void doSnapToBuilding(RaycastHit hit)
     {
         Vector3 insidePoint = hit.transform.parent.collider.ClosestPointOnBounds(hit.point);
 
         Vector3 directionToMouse = hit.point - insidePoint;
         directionToMouse.Normalize();
 
         Vector3 distanceToSnapBound = hit.collider.bounds.max - hit.transform.parent.collider.bounds.max;
         distanceToSnapBound.Scale(directionToMouse);
 
         insidePoint += distanceToSnapBound;
         hit.point = insidePoint;
         doFollowMouse(hit);
     }
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 vladibo · Aug 06, 2017 at 08:50 PM

// extension method

 internal static Vector3 ClosestToSurfacePoint(this Collider collider, Vector3 point, bool throwIfFallback = true)
         {
             var sc = collider as SphereCollider;
             if(sc != null)
             {
                 var closest = sc.ClosestPoint(point);
                 if(fun.distanceSquared.Between(ref closest, ref point) < 0.001)
                 {
                     float len;
                 var toPointDir = (point - sc.transform.position).ToUnit(Vector3.forward);
                 closest = sc.ClosestPoint(sc.transform.position + toPointDir*(sc.radius+0.001f));
                 }
                 return closest;
             }
             var cc = collider as CapsuleCollider;
             if(cc != null)
             {
                 var closest = cc.ClosestPoint(point);
                 if(fun.distanceSquared.Between(ref closest, ref point) < 0.001)
                 {
                     var dir = cc.direction == 0 ? new Vector3(1,0,0) : cc.direction == 1 ? new Vector3(0,1,0) : new Vector3(0,0,1);
 
                     float len;
                     var vec = (cc.transform.rotation * dir) * (cc.height/2f - cc.radius);
                     var c1 = cc.transform.position + vec;
                     var c2 = cc.transform.position - vec;
                     Vector3 drop;
                     fun.point.ClosestOnLineSegment(ref point, ref c1, ref c2, out drop);
                     var toPointDir = (point - drop).ToUnit(out len);
                     closest = cc.ClosestPoint(drop + toPointDir*(cc.radius+0.001f));
                 }
                 return closest;
             }
             if(throwIfFallback) throw new ArgumentException("Unsupported collider type "+collider);
             return collider.ClosestPoint(point);
         }

 //fun.distanceSquared.Between
 public static float Between(ref Vector3 a, ref Vector3 b)
             {
                 var vectorX = (double)(a.x - b.x);
                 var vectorY = (double)(a.y - b.y);
                 var vectorZ = (double)(a.z - b.z);
                 return (float)(((vectorX * vectorX) + (vectorY * vectorY)) + (vectorZ * vectorZ));
             }
 
 public static bool ClosestOnLineSegment(ref Vector3 p, ref Vector3 line1, ref Vector3 line2, out Vector3 closest)
             {
                 point.ProjectOnLine(ref p, ref line1, ref line2, out closest);
                 if (!IsOnSegment(ref line1, ref closest, ref line2))
                 {
                     var d1 = distanceSquared.Between(ref line1, ref closest);
                     var d2 = distanceSquared.Between(ref line2, ref closest);
                     closest = d1 < d2 ? line1 : line2;
                     return false;
                 }
                 return true;
             }
 public static void ProjectOnLine(ref Vector3 point, ref Vector3 line1, ref Vector3 line2, out Vector3 projection)
             {
                 var pointToLine = point - line1;
                 var lineVector = line2 - line1;
                 Vector3 onNormal;
                 vector.ProjectOnNormal(ref pointToLine, ref lineVector, out onNormal);
                 projection = onNormal + line1;
             }
 public static bool IsOnSegment(ref Vector3 segStart, ref Vector3 point, ref Vector3 segEnd)
             {
                 if (point.x <= max(segStart.x, segEnd.x)+epsilon && 
                     point.x >= min(segStart.x, segEnd.x)-epsilon && 
                     point.y <= max(segStart.y, segEnd.y)+epsilon && 
                     point.y >= min(segStart.y, segEnd.y)-epsilon && 
                     point.z <= max(segStart.z, segEnd.z)+epsilon && 
                     point.z >= min(segStart.z, segEnd.z)-epsilon)
                    return true;
  
                 return false;
             }




Comment
Add comment · Show 2 · 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 silentreaver · Nov 26, 2018 at 01:50 PM 0
Share

This code uses methods that don't exist in Unity (toUnit) and needs a lot of rewriting to get close to working (removing variable references to internally referenced methods, like Between and deleting the "distanceSquared" variable)

avatar image vladibo · Nov 26, 2018 at 01:54 PM 1
Share

https://github.com/vbodurov/unity-functions/blob/master/UnityFunctions/Assets/Scripts/fun.cs#L4885

open source project: https://github.com/vbodurov/unity-functions/tree/master/UnityFunctions

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

18 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

Related Questions

Vertex Snapping Produces Gaps 0 Answers

Get position of the centre of each grid tile 1 Answer

How can I consistently get the rectangle bounds around a mesh? 2 Answers

Getting Screen Bounds 1 Answer

Get right or left Vector2/3 out of renderer.bounds 0 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