Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

[SOLVED] Large object going through to small holes (sfw)

Discussion in 'Scripting' started by sebgen, Jan 13, 2016.

  1. sebgen

    sebgen

    Joined:
    Aug 8, 2015
    Posts:
    6
    I'm developing a first person puzzle game much like The Talos Principle. A hugh part of the game is to carry cubes. But if there is a small hole in the wall, smaller than the cube, it goes through. Is there any way to solve this? bug02.png bug01.png

    Thanks,
    Sebastian
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,735
    How do you move the cube currently, script-wise?
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    are you using the built in colliders/physics?

    how are you "carrying" the cubes?
     
  4. sebgen

    sebgen

    Joined:
    Aug 8, 2015
    Posts:
    6
    Code (csharp):
    1.  
    2. void Carry( GameObject o )
    3. {
    4.         o.transform.position = Vector3.Lerp(  o.transform.position, mainCamera.transform.position + mainCamera.transform.forward * distance, Time.deltaTime * smooth );
    5.         o.transform.rotation = mainCamera.transform.rotation;
    6. }
    7.  
     
    Last edited: Jan 13, 2016
  5. sebgen

    sebgen

    Joined:
    Aug 8, 2015
    Posts:
    6
    It works fine (but "jiggy") when there is no hole. But if I carry it near the hole it goes through. Any suggestions?
     
  6. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    You haven't answered an important question.
     
  7. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,735
    1. [code ]code tags[/code]

    2. If you want it to be blocked, you'll want to set its rigidbody's velocity rather than setting its position directly.
     
  8. sebgen

    sebgen

    Joined:
    Aug 8, 2015
    Posts:
    6
    The cube has a riggidboddy and the walls has a box collider.
    When I pick up the cube i turn off it´s gravity
     
  9. sebgen

    sebgen

    Joined:
    Aug 8, 2015
    Posts:
    6
    I will try that immediately. Thank you
     
  10. sebgen

    sebgen

    Joined:
    Aug 8, 2015
    Posts:
    6
    This seems to work, though it may be a bit irritating that the cube can get stuck and hinder the player but it is much better than it going through holes.

    Thanks for the help!

    Code (csharp):
    1.  
    2. void Carry( GameObject o )
    3.     {
    4.         Vector3 targetPosition = transform.position + (mainCamera.transform.forward * distance) + GetComponent<Rigidbody>().velocity;
    5.         Vector3 objPos = o.transform.position;
    6.  
    7.         if( Vector3.Distance( objPos, targetPosition ) > 0.1f )
    8.         {
    9.             Vector3 dir = targetPosition - objPos;
    10.             o.GetComponent<Rigidbody>().velocity = dir * smooth;
    11.         }
    12.         else
    13.         {
    14.             o.GetComponent<Rigidbody>().velocity = Vector3.zero;
    15.         }
    16.         o.transform.rotation = Quaternion.identity;
    17.  
    18.     }
    19.  
     
  11. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148