Unity Community


Results 1 to 13 of 13

Thread: Fish Tank


  1. Posts
    168

    Fish Tank

    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:  
    1. var newPosition : Vector3 = Random.insideUnitCircle * 0.5;
    2. transform.position.x = newPosition.x;
    3. transform.position.y = newPosition.y;   
    4. transform.position.z = newPosition.z;
    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. Posts
    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. Posts
    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. Posts
    702

    Re: Fish Tank

    Quote Originally Posted by apple741
    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:  
    1. var newPosition : Vector3 = Random.insideUnitCircle * 0.5;
    2. transform.position.x = newPosition.x;
    3. transform.position.y = newPosition.y;   
    4. transform.position.z = newPosition.z;
    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?
    Yes - Change the lines

    Code:  
    1. transform.position.x = newPosition.x;
    2. transform.position.y = newPosition.y;   
    3. transform.position.z = newPosition.z;

    to

    Code:  
    1. transform.position.x += newPosition.x;
    2. transform.position.y += newPosition.y
    3. transform.position.z += newPosition.z;

    BigRedSwitch Games Ltd.
    UNITY 4.1/iPhone Advanced 4.1


  5. Posts
    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:  
    1. function OnTriggerEnter (other: Collider)
    2. {
    3.     if(other.gameObject.name == "wall")
    4.    
    5.     {
    6.         FishAI.start =false;
    7.         FishAI.directionAvailable = false;
    8.         print ("Collision With Wall");
    9.        
    10.     }   
    11.     else
    12.     {
    13.         print ("no Collision with wall");
    14.     }
    15.    
    16. }

    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. Location
    Stafford,VA USA
    Posts
    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/documenta...xCollider.html


  7. Posts
    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. Posts
    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. Location
    Berlin
    Posts
    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. Location
    Stafford,VA USA
    Posts
    162
    Hey again apple741 If you're good to go now great, let me know...anyway, to answer this post:

    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.
    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! http://unity3d.com/support/documenta...xCollider.html


  11. Posts
    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. Posts
    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. Posts
    3

    help

    Quote Originally Posted by numeric View Post
    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


    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •