- Home /
Vector3.RotateTowards Problem
Could someone explain why the code from Unity's website regarding Vector3.RotateTowards does not work?
public class ExampleClass : MonoBehaviour
{
// The target marker.
Transform target;
// Angular speed in radians per sec.
float speed;
void Update()
{
Vector3 targetDir = target.position - transform.position;
// The step size is equal to speed times frame time.
float step = speed * Time.deltaTime;
Vector3 newDir = Vector3.RotateTowards(transform.forward, targetDir, step, 0.0f);
Debug.DrawRay(transform.position, newDir, Color.red);
// Move our position a step closer to the target.
transform.rotation = Quaternion.LookRotation(newDir);
}
}
Even a simple box wont rotate what so ever. I really need something like this for my game and even this example will not work.
Could someone explain why or even post a fix or an alternative?
If you're trying to use that code unmodified then seems to me there's an obvious issue in that speed
is private and not assigned to, so will have the default value of zero. I would suggest a) making it public so you can tweak it and b) setting its default value to 1.0.
Having said that, target
is also private and not assigned to. So presumably you're not using the code unmodified as it'd be throwing null reference exceptions.
Answer by Lawrence57 · Sep 09, 2019 at 09:12 AM
I'm trying to achieve specialized movement in a circular direction around a point. For example, the character moves from in front of the point to the right of the point while maintaining their distance from the point. After some struggling with complicated math, I got to thinking I might be able to use Vector3.RotateTowards with the angleRadiansDelta calculated.
hello if you objetc that rotate have a transform you can use Transform.RotateAround and you have solved the problem
Otherwise you need a right, forward, and up vector (if you have just 2, example forward and up, like 3 point plane, you can use Vector3.Cross to get the right)
taking the current angle you can use sine and cosine to get the position on (x,y) relative to the angle
suppose to you will store the desired position in currPos
(current position) variable, point to pivot in centerPoint
and distance to the point in distance
, lets draw it like a vector on a 2d plane formed by the point center (as the 0 on the plane), the right(as the x axis) and forward (as the y axis)
to see the position (x, y) of certain rotation + distance to center on a plane, you need to perform the next calculus, source
(cosine(angle), sine (angle))*distance to center
to do this on 3d plane using forward and right vectors you just have to multiply sine, and cosine results (`float`), by the the axis vectors normalized (`Vector3`) and the multiply the result to the distance, more clearly I think expressed in code
//initialize var
Vector3 currPos = Vector3.zero;
//get the position on x axis (cosine) and multiply it by right axis
//(right direction to the plane on 3d space where the circle rotate)
currPos = right.normalized * $$anonymous$$athf.Cos(angle);
//get the position on y axis (sine) and multiply it to forward axis and sum to the previous
currPos += forward.normalized * $$anonymous$$athf.sin(angle);
//mutiply that by the desired distance around the point
currPos *= distance;
// translate de vector to the center, adding it
currPos += centerPoint;
so, how to get forward and right vectors? if you are rotating around a transform, use the native forward and right (or up if you need)
if you have a axis that you define the normal to the plane where the "circle of rotation" will be "drawed" you can use cross product to get two perpendiculars easy
Vector3 nonParallelVector = axis;
Vector3 right:
Vector3 forward;
//setting a non parallel vector of axis, to do a croos product and get first perpendicular
if($$anonymous$$athf.Approximately(axis.x, 0)) //x == 0 using float...
nonParallelVector.x = 1;
else (){
nonParallelVector.y = nonParalelVector.x;
nonParallelVector.x = 0;
}
right = Vector3.Cross(nonParallelVector, axis); //get perpendicular to two non paralel vectors
forward = Vector3.Cross(right, axis); //get the last perpendicular to the axis vectors
if you need a random forward and right, you can take two different points, get the vectors from center and calculate two cross products, etc