Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[SOLVED] Update stops its execution when the object collide with another one

Discussion in 'Physics' started by Jymmy097, Jan 16, 2015.

  1. Jymmy097

    Jymmy097

    Joined:
    Jul 7, 2014
    Posts:
    67
    Hi everybody,


    EDIT: I've solved this 5 seconds after I've posted this thread... the problem is the if statement which checks whether the ray has hit some collider... it seems that the object which emits the ray goes inside the invisible collider: this is why I cannot get it to work.
    I'm really sorry for that. Can an ADMIN remove my post please?
    Thanks and sorry again.

    I have this scene:
    http://snap.ashampoo.com/ZlD36Wyz
    and there is an invisible collider between the fluid and the cube:
    http://snap.ashampoo.com/2PCmiepY
    I have put this code inside the block:
    Code (CSharp):
    1. public GameObject Fluid;
    2.     public GameObject RayProjector;
    3.     public int FluidDensity = 1000;
    4.     public int BodyDensity = 7000;
    5.  
    6.     public float UnderwaterVolume = 0;
    7.     private float Mass = 0;
    8.     private float BodyVolume = 0;
    9.     void Start()
    10.     {
    11.         BodyVolume = (this.transform.localScale.x * this.transform.localScale.y * this.transform.localScale.z);
    12.         Mass = BodyDensity * BodyVolume;
    13.         rigidbody.mass = Mass;
    14.     }
    15.  
    16.  
    17.     void Update()
    18.     {
    19.         float Archimede = 0;
    20.         RaycastHit hit;
    21.         if (Physics.Raycast(RayProjector.transform.position,Vector3.down, out hit))
    22.         {
    23.             if (hit.collider.gameObject.tag == "Finish")
    24.             {
    25.                 //In water
    26.                 float CylinderHeight = Fluid.transform.localScale.y*2;
    27.                 Debug.Log(hit.distance);
    28.                 float UnderwaterHeight = CylinderHeight - hit.distance;
    29.                 UnderwaterVolume = UnderwaterHeight < transform.localScale.y ? (transform.localScale.x * UnderwaterHeight * transform.localScale.z) : BodyVolume;
    30.                 Archimede = FluidDensity * 9.81f * UnderwaterVolume;
    31.                 rigidbody.AddForce(0, Archimede, 0, ForceMode.Force);
    32.             }
    33.             else
    34.             {
    35.                 Debug.LogWarning("Not inside fluid!" );
    36.             }
    37.             //Debug.Log(string.Format("{0}, {1}", -rigidbody.mass * 9.81f, Archimede));
    38.             Debug.Log("Hello!");
    39.             Debug.DrawRay(RayProjector.transform.position, Vector3.down, Color.red);
    40.         }
    41.  
    42.     }
    It works fine but at the exact moment the block reaches the invisible collider, it stops working: I.E. no force is applied neither the message Hello is logged inside the developer console....

    Can you please help me with that? It realy seems like that the block freezes when it reaches the invisible collider and I do not know why...

    Thanks a lot!

    Jymmy097