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

River Currents using UV Scrolling

Discussion in 'Scripting' started by protopop, Sep 15, 2012.

  1. protopop

    protopop

    Joined:
    May 19, 2009
    Posts:
    1,549
    I have a river mesh with a UV scrolling script that moves the texture down the river and along the curves.

    It also has a mesh collider so when my character collides with it we hear walking in water sounds.

    Now im thinking could I also use the UV somehow to simulate a real current. ie move an object collided with the mesh in the direction of the UV scroll, thus 'flowing it' down the river?

    Im thinking
    a: collide character with mesh
    b: get UV direction at that point
    c: move character in uv direction

    it could also make it harder to walk upstream for example, or ride a raft down a river

    Any one have an answer for this?

     
    Alvarezmd90 likes this.
  2. BlackMantis

    BlackMantis

    Joined:
    Feb 7, 2010
    Posts:
    1,475
    There are many ways to approach this. I would create waypoints(transforms) down the river. If the object lets say fell in the water half way down the river you will need to find the closest waypoint down river and have the player start heading that direction.

    What type system are you using for buoyancy?
     
  3. Brian-Stone

    Brian-Stone

    Joined:
    Jun 9, 2012
    Posts:
    222
    Does it have to be very precise? Is the river a key component of the game, or is it just part of the scenery?

    You should probably avoid mesh collisions if performance is any concern. If you don't need the river current modeled very precisely, then a good (and very easy) way to do this would be to line the river with box colliders attached to empty game objects. Sink the boxes under ground so the top hugs the surface of the river. Rotate each box's Z-axis (forward vector) to point down stream. Then write a script that traps OnTriggerEnter() and sends the message to your character controller class that he is in water and also send the collider's forward vector. Handle the physics in your character controller's update call. Easy as pie.
     
  4. protopop

    protopop

    Joined:
    May 19, 2009
    Posts:
    1,549
    thanks for answering. That makes me think maybe about using a spline to join between waypoints and move teh caharcater along the path, or combine waypoints with the boxes Brian Stone mentions, with the boxes as kinds of waypoints. hmmm.

    No buoyancy in the rivers. they are shallow everywhere so that you are only ever knee deep in them. There are lakes and oceans though. In the lakes i just transition to swimming once you go below a certain Y height, but you can't swim up or down, just kind of long the lake floor. They aren't too deep though.

    For the ocean, i have a trigger that transitions you to basically flying in water so you can swim up or down. It works really well. Before i had you transition to "flying/swimming" below a certain Y but things were jittering back and forth when you crossed that threshold.

    But now i am thinking it is worth it to base the swimming on the way but separate the in and out transitions, so when you are below y 10 use swimming mode and when you are above y 20 use walking, this way they won't interfere with each other.
     
  5. protopop

    protopop

    Joined:
    May 19, 2009
    Posts:
    1,549
    I like this. Im going to look into using box colliders instead of the river mesh. so far performance hasnt been impacted, details are sketchy but it looks like mesh colliders use something called Narrow Phase collision which is more expensive, and boxes use Broad Phase. Many boxes is apparently cheaper, and since i dont use total precision i think it will work great, thanks for the tip.

    For the current, yeah, i guess when you trigger into a box, i would add force in the forward direction of that box to the character, maybe with a bit of random side to side so we dont go in a straight line. In that sense, the front edge of the box acts kind of like the waypoint that Back Mantis mentions above.

    And maybe the force could vary - in places where the water has rapids i could turn the character collider towards the front edge of the box - align the character controller with the forward direction of the box - more strongly, so in a rapid current you would face the forward almost immediately and be dragged along with the water. I dont know how to easily simulate sronger vs weaker currents when the UV offset is pretty much the same across the mesh unless i altered the mesh so that some areas would have longer polygons that made the UV travel across it quicker, im thinking a simpler way is just to add more visuals at those points to simulate faster water, like more splashes, rocks, foam and mist.

    I was hoping that i could access the Scrolling UV, i mean, if the texture is scrolling along the mesh exactly as a current would anyways, why cant we access that and just apply that offset to the character collider? If any others have an idea about this i'm open to that too.

    Anyways both of you have given me some really good ideas - ill try the boxes and let you know how it works! It will probably boost performance too.

    My imagination is fired up - Thank You!
     
  6. Alvarezmd90

    Alvarezmd90

    Joined:
    Jul 21, 2016
    Posts:
    149
    I want this as well. Isn't there a way to get this data from a mesh?
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,711
    Please don't necro-post to ten year old threads with vague expressions of desire for "this data."

    There is no such thing as "scrolling UV" data... you may perform a scrolling operation, you may do it upon UVs, but it's not "this data."

    If "this data" is in the Mesh, you can go to the Mesh documentation and find the property you need.

    If it is something more, start your own post (it's FREE!) and use proper Unity terminology.

    Remember: we cannot read your mind and people from ten years ago may no longer be here.

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220
     
    Last edited: Jun 2, 2022
    Bunny83 likes this.
  8. Alvarezmd90

    Alvarezmd90

    Joined:
    Jul 21, 2016
    Posts:
    149
    I absolutely agree with you on problem reporting. Necro-posting however, I disagree with it.
    Regardless of your rant though, if you'd done your research yourself you'd know that pretty much no information is found on Google about this specific issue to tackle. And like you said, there doesn't seem to be a simple way of doing such a thing. So I kinda hoped to put this particular topic into discussion again. But I guess that's the wrong subforum.
    Anyway, I recall watching YouTube videos a long time ago talking about Mario64 or Ocarina Of Time did that technique to push the player alongside the uv. That's why I have the desire to gain knowledge on that.
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,711
    My bad! I have to agree with you... it does seem that Google is failing. Perhaps the terms are just too generic??

    Anyway, my apologies, you are correct.

    Therefore I made a quick demo of scrolling UVs in my MakeGeo project.

    It shows a straight river and curved river (geometry provided), and has two ways of scrolling the UVs: one by manipulating the material, one by actually changing the UVs in the mesh.

    Look for the
    uvscrolling
    scene in MakeGeo.

    MakeGeo is presently hosted at these locations:

    https://bitbucket.org/kurtdekker/makegeo

    https://github.com/kurtdekker/makegeo

    https://gitlab.com/kurtdekker/makegeo

    https://sourceforge.net/p/makegeo