Thanks in advance for any help. I’ve done a forum search and though there are quite a few posts relating to what I’m trying to do, to my understanding none are quite what I’m looking for. Note: there is no directional player input in this scenario.
My game object has a behavior script on it that moves the object through 3D space on all axis over time (random swooping bird like motions). What I want to do, is have the object rotate to face the direction it is currently moving. For instance, if it is moving towards the north west direction (+z and -x) it should rotate to face north west. If at that point it begins to move down along the y axis it should begin to face down or face the average of all directions it is currently moving in.
So I guess I need check how many degrees the object has moved per each vector every frame, and use the average to tell the object how much to face x y and z? Just not sure how to code that (still grasping quaternions/eulers/rotation stuff). Any help is greatly appreciated. Many thanks.
I still can’t get this to work. Trying to get the object to look in the direction its moving - as if it’s following a spline. Here’s my non working code.
first off you’re constantly setting you’re object’s postion to equal the postion of a target. you don’t check if the target is null so you can get null refs. secondly constantly overriding the objects position likely invalidates the rigidbody’s velocity calculations. remove either the rigidbody or the target, they conflict with each other.
if you use the target positon:
if(!target) return;
direction = target.position - transform.position;
transform.position = target.transform.position;
if you use rigidbody
if(!rb) return;
direction = rb.velocity;
or the most modular approach is to compare current position with last position which will work with either rigidbody or target
Vector3 lastPosition;
//... rest of code
void Start()
{
lastPosition = transform.position;
}
void Update()
{
direction = transform.position - lastPosition;
lastPosition = transform.position;
//... rest for code
}
next check if the direction is not zero. if the object is not moving (or moving fast enough to be visually noticable) there no need to turn it, and avoids unneeded Quaternion math (and thus LookRotation from spamming Debug.Log(“Look rotation is zero”).
//Epsilon is the smallest float you can have thats greater than 0 without loss of float percision,
// if the magnitude is smaller than this then don't rotate
if(direction.sqrMagnitude < float.Epsilon)
return;
Quaternion targetRotation = Quaternion.LookRotation(direction);
float step = speed * Time.deltaTime;
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, step);
if you’re so inclined you can also cache the last direction that wasn’t zero so that the object can continue to rotate towards any direction it jerked towards even when its not moving.
Thanks very much! instead of overriding to the target position every frame, I made it a child of the target. I’m finally seeing the results I’m looking for. However, it is very jumpy, and will sometimes spin out of control. How can I smooth out the rotations?