- Home /
Slerp object in direction of traveling
Hi there guys, i'm having an issue trying to figure out how to keep an object looking the direction it is heading, I have some boats that float around a scene going to and fro random waypoints, i did have the boats looking into the direction they were heading when the waypoints just went up by one number ie, start at 0, next is 1, next is 2, but now that they are randomised, I don't know how to grab that random number and face toward it.
This is the current scripting I have for my boats, the Quaternion Vector3 parts of the script I had removed due them them not being able to identify that said randomised number (or atleast im unsure of how to indentify it within a Quaternion Vector Slerp).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class boatScript : MonoBehaviour {
public Transform[] target;
Transform newTarget;
public float rotationSpeed = 5;
public float speed = 1;
public float accuracyWP = 10;
bool isMoving = false;
void Start () {
}
void Update () {
if (!isMoving)
{
newTarget = target[Random.Range(0, target.Length)];
isMoving = true;
}
transform.position = Vector3.MoveTowards(transform.position, newTarget.position, speed * Time.deltaTime);
//Needs Rotation Here
if (transform.position == newTarget.position)
{
isMoving = false;
}
}
}
Answer by x4637x · Mar 31, 2018 at 08:08 AM
Insert this after line 25 : transform.LookAt(newTarget);
Thank you for the reply, is there anyway i can add a Slerp to this function?
Take a look at Vector3.Slerp, maybe this is what you want.
i think more in what i am after is a Quaternion Slerp sorry, im very new, still looking at API's to understand how it works.
Try using this ins$$anonymous$$d :
line 26 : transform.rotation = Quaternion.LookRotation(Vector3.RotateTowards(transform.forward, newTarget.position - transform.position, rotationSpeed * Time.deltaTime, 0.0f));