There is a Inbuilt function in Unity called Vector3.RotateTowards …
I tried writing this using CrossProduct then use Axis rotation. but didn’t work.
I am wondering if anybody could write Vector3.RotateTowards for me.
Thanks for any help.
There is a Inbuilt function in Unity called Vector3.RotateTowards …
I tried writing this using CrossProduct then use Axis rotation. but didn’t work.
I am wondering if anybody could write Vector3.RotateTowards for me.
Thanks for any help.
Can you just use
MyTransform.LookAt(target.position);
Or can you describe better what you are attempting to do?
So you want a copy implementation of Vector3.RotateTowards(…)?
Why… it already exists… Vector3.RotateTowards(…).
If that’s NOT what you want, then you need to explain what it is you want. Which you haven’t done, because you asked for Vector3.RotateTowards(…).
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation (target.transform.position - transform.position), rotationSpeed * Time.deltaTime);
It can be for practicing purposes, maybe understanding.
Still requires explanation…
If he wanted to understand how it worked, he could go read up on rotation matrices, but I doubt the reason he wants to re-write the function is that. More likely OP has a problem that rotatetowards doesnt do what OP is trying to do…
Hi guys, sorry for the thread necromancy, but… I need the same thing OP did.
I need a Vector3.RotateTowards alternative that is non-linear for the vector direction change ( something like a Vector3.Slerp(directionA, directionB, Time.deltaTime * angularSpeed * Mathf.Deg2Rad) ).
Otherwise it’s just the thing I need.
Here it is:
Vector3 Vector3RotateTowards(Vector3 current, Vector3 target, float maxRadiansDelta, float maxMagnitudeDelta)
{
// replicates Unity Vector3.RotateTowards
float delta = Vector3.Angle(current, target) * Mathf.Deg2Rad;
float magDiff = target.magnitude - current.magnitude;
float sign = Mathf.Sign(magDiff);
float maxMagDelta = Mathf.Min(maxMagnitudeDelta, Mathf.Abs(magDiff));
float diff = Mathf.Min(1.0f, maxRadiansDelta / delta);
return Vector3.SlerpUnclamped(current.normalized, target.normalized, diff) *
(current.magnitude + maxMagDelta*sign);
}