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;
        }
}

1 Answer

1

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.

Actually, I may be wrong about Slerp happening only once, but you'll still need to change target point. I still think a separate function would be cleaner. It's late. I'll think about it some more unless someone better than me gives you the perfect solution :)

Sorry for the delay. Had to work :( I modified the answer above with a script that seems to work okay.

Sorry again for the delay. I don't get notifications of these comments so it's hard to keep up. Yes, tried to set it up so you could have as many way points as you want. By placing them in an array and using the index numbers in you trigger objects, you can direct the boat to whichever one you want next. There are probably better ways to do waypoints, but this seemed fairly simple and it'll do the job.