Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • 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 this scale (3,1,1) I make him rotate around itself but i got the overlap problem because his x scale is 3

Is there anyway to make him 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 this is a special case where your objects are always in this 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 this to work will massively increase the code complexity, and it would then be better to use rigidbodies and joints etc.

The simple solution for this 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 this 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 = this.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.

Follow this Question

Answers Answers and Comments

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

Related Questions

Smooth motion rigidbody 1 Answer

Objects not acting according to physics 1 Answer

Why is my Rigidbody freaking out like this? 0 Answers

stack-type game problem (blocks fall). bloques se caen en juego de apilar cajas 0 Answers

GetAxis being missed in FixedUpdate work around? 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