Fish Tank

Discussion in 'Scripting' started by numeric, Jun 19, 2010.

  1. numeric

    numeric

    New Member

    Joined:
    Jan 31, 2010
    Messages:
    168
    I'm trying to get my fish objects (cubes at the moment) to move forward, backward, left diagonal and right digonal at random in my game.

    The best I could come up with is this:
    Code (csharp):
    1.  
    2. var newPosition : Vector3 = Random.insideUnitCircle * 0.5;
    3. transform.position.x = newPosition.x;
    4. transform.position.y = newPosition.y;  
    5. transform.position.z = newPosition.z;
    6.  
    The problem is that this moves the cube around in a small circle instead of the full constraints of the fish tank.

    Any Ideas on how I can achieve this?
  2. meathelix

    meathelix

    New Member

    Joined:
    Jun 19, 2010
    Messages:
    12
    Might be a bit too technical if you're just getting into programming, but this is a good source on getting things to move around in an organic fashion.
    http://www.red3d.com/cwr/steer/
  3. numeric

    numeric

    New Member

    Joined:
    Jan 31, 2010
    Messages:
    168
    Thanks meathelixm, that looks pretty good but is probably a little over my head at the moment.

    My game is going for a very cartoon type style so this random movement can be as simple as :

    1. pick a random direction of orward, backward, left diagonal or right digonal.

    2.If collision from tank wall is detected, turn around and go back to 1.

    I'm just not too sure how to put it into code.
  4. S7ARBVCK

    S7ARBVCK

    Member

    Joined:
    Feb 11, 2009
    Messages:
    704
    Yes - Change the lines

    Code (csharp):
    1.  
    2. transform.position.x = newPosition.x;
    3. transform.position.y = newPosition.y;  
    4. transform.position.z = newPosition.z;
    5.  
    to

    Code (csharp):
    1.  
    2. transform.position.x += newPosition.x;
    3. transform.position.y += newPosition.y
    4. transform.position.z += newPosition.z;
    5.  
    :)
  5. numeric

    numeric

    New Member

    Joined:
    Jan 31, 2010
    Messages:
    168
    Thanks , I played around with it and am now getting more of a organic feel to the game.

    A problem I'm having now is keeping the fish confined to the tank area. My solution to this is create a collision script for the fish and when ever a trigger is detected between the fish and the wall , move the fish in a new direction.

    The problem is that although I'm in the early stages of setting this up, I cant seem to get the code bellow to print anything to the console:

    Code (csharp):
    1.  
    2. function OnTriggerEnter (other: Collider)
    3. {
    4.     if(other.gameObject.name == "wall")
    5.    
    6.     {
    7.         FishAI.start =false;
    8.         FishAI.directionAvailable = false;
    9.         print ("Collision With Wall");
    10.        
    11.     }  
    12.     else
    13.     {
    14.         print ("no Collision with wall");
    15.     }
    16.    
    17. }
    18.  
    Both the walls and fish have box colliders on them.

    Here is also a link to my fishAI script : http://pastie.org/1012932

    Thanks guys,
  6. BoredKoi

    BoredKoi

    New Member

    Joined:
    Jun 18, 2009
    Messages:
    162
    Hiya apple741 -- if you aren't seeing an Trigger messages and you are running your fish through the wall, then I'm guessing one of the two doesn't have a rigidbody attached to it.

    For this particular example, I'd probably use the following:

    1) Static Colliders for the tank walls
    2) Fish with Sphere Trigger Collider and Rigidbody attached
    3) Use Physics.Raycast periodically to give the fish a sense of where the tank wall is in front of it so you can apply a more organic feel of steering instead of >bonk< run into a wall and then do the turn
    4) Regardless of implementing the Raycast, the combo of static collider + rigidbody trigger collider will get the trigger messages flowing as you're expecting in your example

    Committing the matrix at the bottom to memory will help as well: http://unity3d.com/support/documentation/Components/class-BoxCollider.html
  7. numeric

    numeric

    New Member

    Joined:
    Jan 31, 2010
    Messages:
    168
    Thanks very much BoredKoi that did the trick :)

    I actually dont mind the fish bumping into the walls, I think it might actually look good for the style im trying to go for :)

    One problem I'm having is that Im checking for collisions with the walls using triggers which means the fish currently go through the walls.

    I've tried replacing my "function OnTriggerEnter (other: Collider)" with:

    function OnControllerColliderHit (other: Collider)
    and
    function OnCollisionEnter (other: Collider)

    but they dont seem to pick up collision but the OnTriggerEnter did?

    The walls and fish both have box colliders (I'm working with primative shapes until I can get some basic gameplay setup)

    The fish is also a rigid body.

    Thanks again,
  8. numeric

    numeric

    New Member

    Joined:
    Jan 31, 2010
    Messages:
    168
    I've not had much luck with this, I also tried making my walls rigid bodies which didnt help ( I dont think it made a difference as the walls already had box colluiders on them.)

    Thanks
  9. nantas

    nantas

    New Member

    Joined:
    Feb 11, 2010
    Messages:
    122
    Why not make the tank collider smaller than the tank visual?

    I think your problem is setting the new position of fish directly in your code so the collision check happens afterwards.

    I think you should either make the tank collider smaller or run a position check for fish's target new position before move the fish.
  10. BoredKoi

    BoredKoi

    New Member

    Joined:
    Jun 18, 2009
    Messages:
    162
    Hey again apple741 :) If you're good to go now great, let me know...anyway, to answer this post:

    Rule of thumb here is you must pick one or the other between Collisions and Triggers. If two objects collide, and either has a trigger set, you get no Collision message (hence why the fish sails on through the wall, even though raising a Trigger message).

    Collision Approach:

    Walls = Static Collider
    Fish = Rigidbody Collider

    1) Box Colliders only on the walls, and check the Static checkbox under those game objects
    2) Box Collider on the fish (isTrigger unchecked), and a Rigidbody on the Fish
    3) Move the fish around by applying forces
    4) Physics engine and colliders will keep the fish in the tank

    To verify that setup works you can forget about movement for the moment, just check useGravity on the Rigidbody and see if the fish falls and stops on the bottom tank collider.

    Trigger Approach:

    Walls = Static Collider
    Fish = Kinematic Rigidbody Trigger Collider

    1) Box Colliders only on the walls, and check the Static checkbox under those game objects
    2) Box Collider on the fish (check isTrigger), Rigidbody on the Fish (check isKinematic)
    3) Move the fish around by modifying its transform.position
    4) You are responsible for keeping the fish in the tank (i.e., when a trigger message fires clamp its position to keep it from advancing further in whatever x/y/z axis)

    Hope that helps; when all else fails, go back to the matrix at the bottom. It don't lie! 8) http://unity3d.com/support/documentation/Components/class-BoxCollider.html
  11. numeric

    numeric

    New Member

    Joined:
    Jan 31, 2010
    Messages:
    168
    @ nantas: Thanks, I'll need to look into that some more :) .

    @BoredKoi: Thanks very much thats definately cleard a lot up. Great reply :)
  12. numeric

    numeric

    New Member

    Joined:
    Jan 31, 2010
    Messages:
    168
    Sorry for all these questions.

    At the moment Im trying to check what the last direction was after a collison so the Fish AI can move the fish in a new random direction, the problem is I keep confusing the logic in my code.

    Could someone take a look:
    FishAI : http://www.pasteit4me.com/766001
    that script gets collision data from : http://www.pasteit4me.com/754002
  13. seba88

    seba88

    Member

    Joined:
    May 23, 2013
    Messages:
    3


    hello, I'm new to unity and I'm trying to move a fish in the aquarium, but there are still successful.
    can you sent me the complete script for the random miovimento fish with the collision, thank you very much.