# how to rotate boat when it reaches certain point?

I want to rotate my boat when it reaches certain point (Point B) so that it can turn backwards to Point A or starting point. The script that I have is the boat doesn't rotate when it reaches point B, it just move backwards without rotating. I created a cube as my Point B, and attached it to the script of the boat. Another question is how could I control the speed of rotation?.

This is the boat script:

``````var pointB : Transform;
private var pointA : Vector3;
var speed = 1.0;
var rotateup = true;

function Start () {
pointA = transform.position;
while (true) {
var i = Mathf.PingPong(Time.time * speed, 1);
transform.position = Vector3.Lerp(pointA, pointB.position, i);
yield;
}
}

function Update () {

if (rotateup){
for (t = 0.0; t <= 5.0; t += Time.deltaTime){
transform.Rotate(0.1*Time.deltaTime,0,0);
transform.Rotate(0,0,0.1*Time.deltaTime);
}
rotateup = false;
return;
}

if (!rotateup){
for (t = 0.0; t <= 5.0; t += Time.deltaTime){
transform.Rotate(-0.1*Time.deltaTime,0,0);
transform.Rotate(0,0,-0.1*Time.deltaTime);
}
rotateup = true;
return;
}
}

``````

This script goes on your boat.

``````var speed: int = 1;
var turnRate: int = 1;

var boatTarget : Transform []; // this is an array. Set the length to 2 and drop your waypoints into the variable slots.

function Start()
{
TurnBoat (0);
}

function Update ()
{
transform.Translate(Vector3.forward*speed*Time.deltaTime);
}

function TurnBoat(targetPoint : int)
{
var rotation = Quaternion.LookRotation(boatTarget[targetPoint].position - transform.position);
var t = 0.0;
while (t < 1.0)
{
t += Time.deltaTime*turnRate;
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, t);
yield;
}
}

``````

This script goes on each of two trigger objects (colliders set to "is trigger")

``````function OnTriggerEnter (hit : Collider)
{
if (hit.GetComponent (BoatMover)) // BoatMover is the name of the script attached to the boat.
{
hit.GetComponent(BoatMover).TurnBoat(0);
}
}

``````

These objects are your waypoints. The boat should travel between them.