- Home /
Momentum for fps for boat drift
I have a fps that represents sitting in a boat traveling through water. I would like the boat to continue drifting forward or back a little even after the boat stops. I know that the fps is designed to work without momentum and stop instantly but I wanted to build some realistic movement in. (This is for a project that communicates between a sculptural rowboat and the unity3d water environment using physical computing methods to get the signals in as well as with arrow keys.)
I captured a reading of transform.direction.z at the exact time that the speed of the boat changed from 1(forward) to 0 and -1(reverse) to 0(stopped). Then I ran a function with Mathf.SmoothDamp to drift the boat to the transform.direction.z + 5. It works as a method except that the direction that the boat drifts in varies based on the direction the boat is moving. If moving in one direction, the boat might stop and drift forward which makes sense with momentum. However, if it is moving in another direction, it might drift sideways (I guess based on the z situation).
So my question is: How can I keep it so when the boat stops, it always drifts with momentum in the direction the boat was moving in?
Any advice or scripts would be welcomed. Thanks! By the way, this is my first post.
Here is what I am writing: var myFlag: boolean = false; var smoothTime = 0.3; var myDrift: float = 5; private var myStartZ: float; private var myEndZ: float; private var zVelocity = 0.0;
function Update(){ …
if (boatSpeed == 0 && prevBoatSpeed == 1){ myFlag = true; //take a reading of the current position myStartZ = transform.position.z; myEndZ = myStartZ + myDrift;
}
if (boatSpeed == 0 && prevBoatSpeed == -1){
myFlag = true;
//take a reading of the current position
myStartZ = transform.position.z;
myEndZ = myStartZ - myDrift;
}
else if (boatSpeed > 0 || boatSpeed < 0) {
myFlag = false;
}
Drift();
}
} function Drift(){ if(myFlag){ var newPosition : float = Mathf.SmoothDamp(transform.position.z, myEndZ, zVelocity, smoothTime); transform.position = Vector3(transform.position.x, transform.position.y, newPosition); } }