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;
}
}
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 :)
– tool55Sorry for the delay. Had to work :( I modified the answer above with a script that seems to work okay.
– tool55Sorry 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.
– tool55