Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
11 Jun 22 - 14 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 CubePhysics · Jul 11, 2015 at 12:37 PM · gameobjectrigidbody2dvector2split

How would you check if an object is closer to the bottom side or top side of another object

In my game i am working on the enemies AI and I want in c# that whenever specific object comes within a certain distance, to move upwards if object is closer to the bottom side of the enemy and move down if the object is closer to the top of enemy.

     if (GameObject.Find("Laser(Clone)") != null)
         {
             float projectileDistance = Vector2.Distance(this.transform.position, GameObject.Find("Laser(Clone)").transform.position); 
             Debug.Log(projectileDistance);
             if(projectileDistance <=4.8)
             {
                 rb.AddForce(transform.up * speed * Time.deltaTime);
             }
         }//this is attached to the enemy gameobject



So basically i want to know if there is a way of like splitting an object into the top half and bottom half and detect wether another object is closer to which side.

Comment
Add comment · Show 3
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 CubePhysics · Jul 11, 2015 at 02:39 PM 0
Share

bump. Can someone please help

avatar image NoseKills · Jul 11, 2015 at 07:33 PM 0
Share

What kind of objects are we talking about? Something modelled in a 3D modeling software or cubes or what? If the objects are symmetrical and y-axis represents "height", you can just compare their y-coordinate. If object A has a greater transform.position.y than object B, then A is closer to the top of B...

avatar image Cherno · Jul 12, 2015 at 10:17 AM 0
Share

I would just compare the transform.position of object a with one of the Bounds values of object b (Bound.$$anonymous$$ and Bounds.max).

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Tekksin · Oct 14, 2018 at 05:48 AM

something like

 public float speed;
 public Transform[] activeObjects;
 public float[] dist;
 
 void Update(){
   for (i int = 0; i < activeObjects.Length; i++){
       dist[i] = Vector3.Distance(transform.position, activeObject[i].position);
       if(i != 0){
         if(dist[i] < dist[i-1]){
           transform.position = Vector2.Lerp(transform.position, activeObject[i].position, Time.deltaTime * speed);
         }else{
            transform.position = Vector2.Lerp(transform.position, activeObject[i-1].position, Time.deltaTime * speed);
        }
     }
   }
 }

I haven't tested this, but it should work. Attach it to the object that is going to move toward the closest objects.

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

24 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 avatar image avatar image avatar image

Related Questions

Cannot implicitly convert type `UnityEngine.Vector2' to `float' 1 Answer

Object jitters when the scene starts 0 Answers

Rigbody 2D Velocity Mystery 1 Answer

toss up game object into air with freefall physics 1 Answer

Vector2 - Check Position 2 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