- Home /
How Do I Make A River With A Current?
I have a standard Unity First Person Controller, and a cube in some terrain made to look like a river. How would I go about making it so when the player walks into the river, they are pushed in a certain direction? Like if the river had a current and wasn't just an aesthetic piece of scenery?
$$anonymous$$ore difficult than it sounds. First, how do you know when the player is "in" the river - do you have a trigger, are you using physics/colliders, etc. Second, what direction? Is the river a fairly straight affair moving in a generally constant direction, or does it twist and turn?
I'm trying to use a trigger, and the river is flowing in one general direction, with no major turns.
Simple- use transform.Translate(Vector3.forward * Time.deltaTime); you could use left ins$$anonymous$$d of forward for the vector3 Or you could slerp the position using slerp. You could also push the character with an invisible box collider told to move in the direction you want
I see a problem using a Vector3.forward or right/left, as the player turns so to does the direction the current is pushing in so that if I start moving right to left/downstream and then turn around, I'm now pushed "upstream".
Answer by getyour411 · Jan 25, 2014 at 06:35 AM
@superventure had a good suggestion after all, I just forgot to put Space.World in my Translate; once I did that it worked pretty well.
I placed a large cube/as a trigger sized approx to the river and added script
using UnityEngine;
public class QM_Current : MonoBehaviour {
void OnTriggerStay (Collider other) {
if(other.tag == "Player")
other.transform.Translate(-Vector3.right * Time.deltaTime, Space.World);
}
}
(for a current going right to left, change the Vector3.dir to suit your needs.
I tried adding this script as both a javascript and c#, but they both give errors. changing the 'void' to 'function' in javascript makes it compile, but it then does nothing. putting it in c# causes it to throw a namespace error.
this is a snippet and doesn't contain the necessary public class blahblah using blahblah stuff; add that
Edit: I updated it for copy/paste
That would make sense, I'll test it out once Unity finishes updating.
EDIT: I tested the script and it worked great! There is one problem though, it appears that if the player is pushed into terrain in a certain direction, they fall through it. Is that the script or is that just a little thing with the Unity engine?
Script. $$anonymous$$oving things with transform.XXXX (tran/pos) can cause terrain fall throughs, you could add something to lock the position.Y that's usually the culprit. Are you using the standard First Person Controller in your overall game design, as is with no changes/new colliders/etc planned? $$anonymous$$nowing more about your game mechanics and ultimate player setup should deter$$anonymous$$e the route you should go in.
I'm using the default first person controller, it has an extra object parented to the camera (a torch with particles and a point light, but that doesn't collide with anything), it's got 2 GUI displays and a pause script attached to it, and it's got a script to allow it to collect items like coins. No colliders or anything else besides the above has been changed, and as far as I can tell, I won't be modifying it further.