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
5
Question by sam32x · Sep 20, 2012 at 03:15 AM · transformpositionsomething

calculating the midpoint of two objects

i am trying to make an object go to the midpoint of two characters in my game and stay there but its not working, here's my script:

 var mark : Transform;
 var josh : Transform;
 
 function Update () {
 transform.position.x = josh.position.x + (mark.position.x - josh.position.x /2);
 transform.position.y = josh.position.y + (mark.position.y - josh.position.y /2);
 transform.position.z = josh.position.z + (mark.position.z - josh.position.z /2);
 }

whenever i start the game the sphere just goes way off screen, does anyone know what im doing wrong?

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

4 Replies

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

Answer by cowlinator · Sep 20, 2012 at 03:50 AM

In programming and math, division is always evaluated before subtraction.

 (mark.position.x - josh.position.x /2)

is exactly the same as

 mark.position.x - (josh.position.x /2)

So instead, you should use

 transform.position.x = josh.position.x + (mark.position.x - josh.position.x) / 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 adham dwikat · Jun 02, 2015 at 09:23 PM 0
Share

this is true !!! but i cant understand why ???

mid point between A and B is (A+B)/2

why it is A+(B-A)/2 !!!!!!

avatar image cowlinator · Jun 04, 2015 at 06:18 AM 2
Share

A+(B-A)/2 = A+(B/2)-(A/2) = (A-A/2)+(B/2) = (1A - (1/2)A) + (B/2) = (1/2)A + (B/2) = (A/2) + (B/2) = (A+B)/2

avatar image Bunny83 · Jun 04, 2015 at 07:46 AM 2
Share

Right, the approach (A+B)/2 is a more "mathematical approach" as it's just the arithmetic mean.

The approach A+(B-A)/2 is more a "vectorial approach". "A" is the starting point "(B-A)" is a relative direction vector from the start (A) to the end (B). Adding that vector back to our starting point we get to the end (B). By taking half of that vector we only move half way towards B. So the resulting point is exactly halfway between A and B.

The great thing about that approach is that you're not limited to the midpoint. You could easily calculate 1/3 or 2/3 of the distance which doesn't work with the arithmetic mean.

avatar image
7

Answer by DarkMatter · Sep 20, 2012 at 03:49 AM

The midpoint of A and B is (A+B)/2. You have A+B-A/2;

Try: transform.position = (josh.position + mark.position)/2;

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
5

Answer by MennoB · Aug 09, 2021 at 09:02 PM

Since this is still the first answer I found on Google in 2021, I decided to add a shorter solution, using Unity's Vector3.Lerp() to interpolate between two positions.

 transform.position = Vector3.Lerp(josh.position, mark.position, 0.5f);

This method could also be used for any division other than 1/2.

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
2

Answer by SteenPetersen · Jul 28, 2017 at 11:59 PM

In my current project, my enemies need to judge the distance to 2 different vector position on the players. in order to determine if they are facing them or not. But I want them to "target" and run to a point between those two vectors. Here is how I did that:

 [Range(0,1)]
 public float myFloatVariable;  // can precisely place the spot to "target" in editor.
 public Vector3 drawPointOnCurrentTarget;  
 
   Vector3 a = new Vector3(GameObjectRepresentingFace.x, GameObjectRepresentingFace.y);
   Vector3 b = new Vector3(GameObjectRepresentingBody.x, GameObjectRepresentingBody.y);
   drawPointOnCurrentTarget = a + ((b - a).normalized * myFloatVariable);
   float distanceToTarget = Vector3.Distance(drawPointOnCurrentTarget, transform.position);
   Debug.DrawLine(drawPointOnCurrentTarget, transform.position, Color.red);

Hope this helps someone stumbling onto this question.

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

15 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

Related Questions

object rotates when moving backward 1 Answer

Moving gameobject relative to resolution 1 Answer

Setting Position of Spawned Prefab 2 Answers

Difference between transform.forward and transform.position.z? 1 Answer

Change position of camera on scene load? 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