- Home /
Sailing Ship Control Script Help
I have been playing with a script i found here on the unity forums that is for controlling a ship.
Found Here: http://forum.unity3d.com/threads/2278-Sailing-Ship-Control
I was able to make one adjustment i needed, which was very simple, just removing a small snip of code.
But there are some more adjustment's i need, and i have no idea how to do it, i've tried everything i could think of but no luck.
I need two things
When you stop pushing the "Horizontal" input, i want it to stop turning the ship, instead of continuing to turn.
I want it so when you push the "Vertical" input (just once, no need to hold the button down), it slowly increases the speed to max speed, and pushing it opposite to the way the ship is moving (so if the ship is going forward, pressing "S"), will stop the ship/move the ship backwards.
How would this be accomplished?
Sorry, I'm kinda a noob with this stuff, i am still learning =/
Here's the code:
var speed = 1.0;
var acceleration = 1.0;
var maxspeed = 2.0;
var minspeed = -0.25;
var heading = 0.0;
var rudder = 0.0;
var rudderDelta = 2.0;
var maxRudder = 6.0;
var bob = 0.1;
var bobFrequency = 0.2;
private var elapsed = 0.0;
private var seaLevel = 0.0;
private var rudderControl;
private var rudderAngle = 0.0;
private var startPosition : Vector3;
function signedSqrt( x ){
var r = Mathf.Sqrt(Mathf.Abs( x ));
if( x < 0 ){
return -r;
} else {
return r;
}
}
function Update () {
// Bobbing
elapsed += Time.deltaTime;
transform.position.y = seaLevel + bob * Mathf.Sin(elapsed * bobFrequency * (Mathf.PI * 2));
// Steering
rudder += Input.GetAxisRaw("Horizontal") * rudderDelta * Time.deltaTime;
if( rudder > maxRudder ){
rudder = maxRudder;
} else if ( rudder < -maxRudder ){
rudder = -maxRudder;
}
heading = (heading + rudder * Time.deltaTime) % 360;
// transform.Rotate(0, rudder * Time.deltaTime, 0);
transform.eulerAngles.y = heading;
transform.eulerAngles.z = -rudder;
// Sail
speed += Input.GetAxis("Vertical") * acceleration * Time.deltaTime;
if( speed > maxspeed ){
speed = maxspeed;
} else if ( speed < minspeed ){
speed = minspeed;
}
transform.Translate(0, 0, speed * Time.deltaTime);
}
function Awake (){
seaLevel = transform.position.y;
rudderControl = GameObject.Find("rudderControl");
}
Is there still a need for the vertical move stop part?
Answer by StonedLover · Jul 14, 2017 at 06:09 PM
if (Input.GetAxisRaw("Horizontal") != 0)
rudder += Input.GetAxisRaw("Horizontal") * rudderDelta * Time.deltaTime;
else if (rudder >= 0 && Input.GetAxisRaw("Horizontal") == 0)
rudder -= rudderDelta * Time.deltaTime;
else if (rudder <= 0&& Input.GetAxisRaw("Horizontal") == 0)
rudder += rudderDelta * Time.deltaTime;
Thats how you do the stopping
yeah lil bit late but maybe someone need it