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 sandwichmenno · Jun 14, 2014 at 12:50 PM · positionsizepointcenter

Furthest point of object in center of another object

For my current Unity game, I need to set a point of the spawned object, in the center of an another object. I don't really know how to explain it, but I will try to do it with images, because it's a lot quicker and more clear.

When I spawn my object, the object's size is decided by the distance between two objects. It addapted it's size to the distance, as seen in this image.

But, as you can see, the center of the spawned object, is in the center of the round object, but the furthest point (The red circled point), needs to be in the center.

I use this code at the moment;

 #pragma strict
 static var iscreating = false;
 
 var Routepoint: GameObject;
 var Startpoint: GameObject;
 
 var c1 : Color = Color.blue;
 var c2 : Color = Color.blue;
 
 var vehicle : Transform;
 var routePathObject: Transform;
 
 private var line : LineRenderer;
 
 function Update() {
     Routepoint = GameObject.FindWithTag ("Routepoint");
     Startpoint = GameObject.FindWithTag ("Startpoint");
 }
 
 function OnGUI () {
     if (iscreating == true) {
         if (GUI.Button (Rect (240,40,120,20), "Confirm")){
             var routePath : Transform = Instantiate(routePathObject, Startpoint.transform.position, transform.rotation);
             var distance = Vector3.Distance(Startpoint.transform.position, Routepoint.transform.position);
             routePath.LookAt(Routepoint.transform);
             routePath.transform.localScale.z = distance;
             routePath.transform.position = Startpoint.transform.position;
 
             globalinfo.totalroutes += 1;
             iscreating = false;
         }
         
         if (GUI.Button (Rect (240,70,120,20), "Cancel")){       
             iscreating = false;
         }
     }
 }


I have no idea how to do this. I tried to transform the position after the size was set, but it didn't work. I also tried to set a different pivot point of the spawned object, but because the size is different everytime, it didn't work.

I would appreciate it if someone knows the answer.

Comment
Add comment · Show 8
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 LMan · Jun 14, 2014 at 01:48 PM 0
Share

I don't have time to provide a proper detailed answer, so I'll leave my thoughts as a comment, and perhaps they'll be of some help.

What you want is to find the position that is halfway between both objects- this can be done by simply subtracting the transform positions of the two objects, and dividing your answer by 2.

Now you need to know how many units your object takes up to begin with, as scale is relative to the original object. You already know the distance in units the object needs to cover from subtracting the positions, so now it's a matter of adjusting the scale so that the object fits in between.

for instance, if you were using a cube, those are 1 unit big. So you could just multiply 1 by the distance and get what you want. for objects that are not 1 unit to begin with, I think you'll need to subtract the units that they already cover from the distance.

avatar image sandwichmenno · Jun 16, 2014 at 11:37 AM 0
Share

Thanks for the answer. I get the first part of your answer. It's quite logical.

But I didn't get the last part of your answer, as for the units. What I can find out of your answer, I need to subtract the positions of both objects and multiply it with the 'units'?

avatar image ProtoTerminator · Jun 16, 2014 at 12:38 PM 0
Share

When you drag the object into the scene normally, how big is it? If it's 2 units wide, you'll need to multiply the scale by the distance and divide by 2. if it's 5 units wide, divide by 5, etc.

This makes me wonder if there's a built in function to deter$$anonymous$$e an object's 100% scale size in units.

avatar image sandwichmenno · Jun 27, 2014 at 12:08 PM 0
Share

Thanks for the answer.

Where / how can you see the total Units of an object? It's not a built in function if I recall.

avatar image Kiwasi · Jun 27, 2014 at 12:12 PM 0
Share

The easiest way is to make a unit cube and compare

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Locking the mouse cursor without it centering after unlocking it? 0 Answers

How to get position of multiple platforms? 0 Answers

Precise vertex positions 1 Answer

Camera rotation around player while following. 6 Answers

Find center between 4 points 3 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