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

make objects float around within box trigger

Discussion in 'Scripting' started by stefan_, Nov 22, 2014.

  1. stefan_

    stefan_

    Joined:
    Aug 19, 2014
    Posts:
    1
    hi everybody,

    I need help with what seems to be a fairly easy task. I think I could manage programming it myself but looking at my (unfinished) attempts made me think there has to be a more elegant, more efficient way.

    In my scene there is a box trigger (a cube, no mesh, no rigidbody), let's call it bounding box for now. I have some smaller rigidbody objects which can be picked up and carried around by the player. When the player moves one of them near the bounding box and it touches it, the object should float around within that bounding box (on all axis) in a random manner but never leave the bounding box (unless the player later comes back to pick it up from within it).

    How would you go about that? Is the approach of using a box trigger good at all? What functions should I use to achieve a random movement within that cube (or otherwise predefined) volume? Should I apply forces to the smaller objects or have some sort of paths within my bounding box they would move along?

    Thanks a heap for sharing any ideas or hints.

    kind regards, stefan
     
  2. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    683
    I'm not sure of the exact code you need to write but you need to use the OnTriggerEnter and OnTriggerExit classes. In the Enter class you'll want to AddForce to the rigidbody of the object the player puts inside the bounding box to push the box up. To make it rotate around (as you said "on all axis") you'll AddTorque to the same rigidbody as well.

    This TUTORIAL should get you started in the right direction.