# Vehicle: Boat accelerating and turning in Unity

I'm trying to make a player-controllable boat in Unity and I'm running into problems with my code.

1) I want to make the boat to accelerate and decelerate steadily instead of simply moving the speed I'm telling it to right away.

2) I want to make the player unable to steer the boat unless it is moving.

3) If possible, I want to simulate the vertical floating of a boat during its movement (it going up and down)

My current code (C#) is this:

using UnityEngine;
using System.Collections;

public class VehicleScript : MonoBehaviour {
public float speed=10;
public float rotationspeed=50;

// Use this for initialization
// Update is called once per frame
void Update () {

// Forward movement
if(Input.GetKey(KeyCode.I))
speed =
transform.Translate (Vector3.left*speed*Time.deltaTime);

// Backward movement
if(Input.GetKey(KeyCode.K))
transform.Translate (Vector3.right*speed*Time.deltaTime);

// Left movement
if(Input.GetKey(KeyCode.J))
transform.Rotate (Vector3.down*rotationspeed*Time.deltaTime);

// Right movement
if(Input.GetKey(KeyCode.L))
transform.Rotate (Vector3.up*rotationspeed*Time.deltaTime);

}
}


In the current state of my code, when I press the specified keys, the boat simply moves 10 units/sec instantly, and also stops instantly. I'm not really sure how to make the things stated above, so any help would be appreciated. Just to clarify, I don't necessarily need the full code to implement those features, I just want to know what functions to use in order to achieve the desired effects.

Thank you very much.

• You make look into rigidbody.addForce() / rigidbody.addTorque(). I'm currently not technically fitted to write a full answer, but will maybe do so later. – J_F_B_M Aug 21 '14 at 20:47

Don't directly translate the object in response to key presses. Instead, increase the speed in response to key presses, and then translate the object's position using that speed. That way the speed changes every frame. pseudocode:

float speed = 0f;
float maxSpeed = 20f;
float accel = .2f;

void Update() {
if (Input.GetKey(KeyCode.I)) {
speed += accel;
}
if (speed > maxSpeed) {
speed = maxSpeed;
}
transform.Translate(speed);
}

• Thank you very much, that fixed the problem with my code! – Emilios S. Aug 24 '14 at 16:20