- Home /
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;
}
}
}
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;
This is the sort of thing I've been trying.
other.GetComponent().RigidbodyConstraints=RigidbodyConstraints.FreezeAll;
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;
other.GetComponent().RigidbodyConstraints=RigidbodyConstraints.FreezeAll;
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;
}
}
Yes, I don't want them to move at all once they touch.
I'm evaluating/testing your answer now.
Thank you.
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
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.
Answer by rrabi · Aug 14, 2017 at 01:45 AM
Hi, a simple way can be attaching a fixed joint on Collision enter.
I'm still looking at options for this and looking at the responses I'm getting and I very much appreciate your help.