• 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 /
  • Help Room /
avatar image
1
Question by Vadol · Mar 26, 2018 at 01:37 PM · physicsrigidbodyoverlapoverlappinggamobject

How to avoid overlapping of GameObject without rigidbody?

I was looking for a way to make my gameobject not overlap to the another gameobject but all the solutions are talking about Rigidbody..


when i want to do it in script only, is it possible ? I have a cube with t$$anonymous$$s scale (3,1,1) I make $$anonymous$$m rotate around itself but i got the overlap problem because $$anonymous$$s x scale is 3

Is there anyway to make $$anonymous$$m move away automatically to avoid the red gameobject?


alt text

ezgif-4-f2d76dcd9f.gif (237.6 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Scribe · Mar 26, 2018 at 08:47 PM

If t$$anonymous$$s is a special case where your objects are always in t$$anonymous$$s sort of relative location and just want to avoid overlapping with one other specific object then you could code a reasonably simple solution yourself. However any variation in how you want t$$anonymous$$s to work will massively increase the code complexity, and it would then be better to use rigidbodies and joints etc.

The simple solution for t$$anonymous$$s setup could be to find the bounding box of your rotated object in worldspace, and offset your position based on the diff between the current world bounding box and the starting position world bounding box, here's some working, but improvable code!

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
 
 public class HomeGrownPhysics : MonoBehaviour {
 
     Transform t;
     List<Vector3> localCorners;
     float xLock;
     float xPos;
 
     // Use t$$anonymous$$s for initialization
     void Start () {
         CalculateLocalValues();
         xLock = CalculateWorldBounds(t).extents.x;
         xPos = t.position.x;
     }
     
     // Update is called once per frame
     void Update () {
         t.RotateAround(t.position, Vector3.forward, Time.deltaTime * 10.0f);
 
         Bounds b = CalculateWorldBounds(t);
         t.position = new Vector3(xPos + xLock - CalculateWorldBounds(t).extents.x, t.position.y, t.position.z);
     }
 
     private void CalculateLocalValues()
     {
         t = transform;
         Bounds bounds = t$$anonymous$$s.GetComponent<MeshFilter>().mesh.bounds;
 
         Vector3 centre = bounds.center;
         Vector3 M__ = new Vector3(bounds.extents.x, 0, 0);
         Vector3 _M_ = new Vector3(0, bounds.extents.y, 0);
         Vector3 __M = new Vector3(0, 0, bounds.extents.z);
 
         Debug.Log("Centre " + centre);
 
         localCorners = new List<Vector3> {
             centre - M__ - _M_ - __M
             ,centre - M__ - _M_ + __M
             ,centre - M__ + _M_ - __M
             ,centre - M__ + _M_ + __M
             ,centre + M__ - _M_ - __M
             ,centre + M__ - _M_ + __M
             ,centre + M__ + _M_ - __M
             ,centre + M__ + _M_ + __M
         };
     }
 
     private Bounds CalculateWorldBounds(Transform transform)
     {
         Matrix4x4 transformation = transform.localToWorldMatrix;
 
         List<Vector3> corners = localCorners.Select(x => transformation.MultiplyPoint3x4(x)).ToList();
         Vector3 min = corners.Aggregate((a, b) => Vector3.Min(a, b));
         Vector3 max = corners.Aggregate((a, b) => Vector3.Max(a, b));
 
         Vector3 diff = max - min;
         return new Bounds(min + (diff/2.0f), diff);
     }
 
 }

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Related Questions

AddForce in Coroutine 1 Answer

Rigidbody.isKinematic teleporting to ground after toggle 0 Answers

Pick Up and Drop Script not functioning due to loophole 1 Answer

Is there a way to limit animation on collision? 1 Answer

Why Does Bounciness Affects Rigidbody Velocity? (SOLVED) 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