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 /
avatar image
0
Question by Topthink · Aug 13, 2017 at 06:50 PM · collisionmovementcollide

How Do I Freeze Position ?

I have two cubes and when they collide, I would like for them to remain in contact.

However, they eventually break contact on their own. So, I've tried to prevent them from separating by freezing their positions. But, alas, it doesn't seem to be working. I've tried this in "OnCollisionEnter" and I've tried it in "OnCollisionStay" but it doesn't seem to work in either place.

The code follows...might someone offer a suggestion to keep my cubes from separating on their own?

Thank you in advance.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Combat02 : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
         
     }
 
     void OnCollisionEnter(Collision col)
     {
         if (col.gameObject.tag == "Ally") 
         {
             
             col.gameObject.GetComponent<Renderer> ().material.color = Color.cyan;
             //col.rigidbody.constraints = RigidbodyConstraints.FreezePosition; 
 
         }
     }
 
     void OnCollisionStay(Collision col)
     {
         if (col.gameObject.tag == "Ally") 
         {
             col.rigidbody.constraints = RigidbodyConstraints.FreezePosition;
         }
     }
 }
 
 
Comment
Add comment · Show 5
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 Topthink · Aug 13, 2017 at 09:27 PM 0
Share

I'm trying all sorts of syntax such as the following but I'm not getting anything accomplished. I've tried to look up the syntax but I can't find anything. Anyone?

other.GetComponent().RigidbodyConstraints=RigidbodyConstraints.FreezeAll;

avatar image Topthink · Aug 13, 2017 at 09:28 PM 0
Share

LoL. It didn't print that they way I pasted it.

avatar image Topthink · Aug 13, 2017 at 09:29 PM 0
Share

This is the sort of thing I've been trying.

other.GetComponent().RigidbodyConstraints=RigidbodyConstraints.FreezeAll;

avatar image Topthink · Aug 13, 2017 at 09:31 PM 0
Share

Okay, as soon as I press enter, it's changing my post. Not sure what to do. I'll try it one more time.

 other.GetComponent<Rigidbody>().RigidbodyConstraints=RigidbodyConstraints.FreezeAll;

avatar image Topthink · Aug 13, 2017 at 09:32 PM 0
Share

other.GetComponent().RigidbodyConstraints=RigidbodyConstraints.FreezeAll;

2 Replies

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

Answer by Course-Interactive · Aug 13, 2017 at 10:58 PM

Hi Topthink

Enabling the "FreezePosition" of the Rigidbody basically prevents the GameObject from moving at all. If you do this to both of them, they won't move. If you do it to one.. this one won't move and the second one moves away from the first one. That's not your way to go.

To move both of them together you could make one of them a child of the other. Disabling the Collider should prevent them from colliding again.

 void OnCollisionEnter(Collision col)
  {
          if (col.gameObject.tag == "Ally") 
          {
                  col.gameObject.GetComponent<Renderer> ().material.color = Color.cyan;
                  col.gameObject.transform.parent = transform;
                  col.enable = false;
          }
 }
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 Topthink · Aug 14, 2017 at 12:05 AM 0
Share

Yes, I don't want them to move at all once they touch.

I'm evaluating/testing your answer now.

Thank you.

avatar image Lunezo Topthink · Aug 14, 2017 at 12:20 AM 0
Share

Not sure if your looking for something specific but if your Box/Boxes are static background items that you need or want physics but don't want them to move Unity has a built in Feature in the Rigibody component called ">Contraints" or you can make it $$anonymous$$inematic

avatar image Topthink · Sep 14, 2017 at 10:40 PM 0
Share

I tried what you suggested and what you have there seems to work well.

I ended up doing this a slightly different way but I appreciate you taking the time to help me.

Credit to you just the same.

Thanks again.

avatar image
0

Answer by rrabi · Aug 14, 2017 at 01:45 AM

Hi, a simple way can be attaching a fixed joint on Collision enter.

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 Topthink · Aug 14, 2017 at 03:26 PM 0
Share

I'm still looking at options for this and looking at the responses I'm getting and I very much appreciate your help.

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

130 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

Related Questions

Unity 3D C# - Collosion Code Not working? 3 Answers

How to stop a character from moving out of player's control? 0 Answers

How to move a Game Object from a script not attached to it. 1 Answer

Boat collides with track(plane) below it while moving forward 0 Answers

Simple moving ball script 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