- Home /
Rotation is jumpy
hello everyone here's my script:
if(!buScript.around(myTransform.position, this.pos, 0.5f)) {
int _turn = 0; // left = -1; none = 0; right = 1;
Vector3 dir = (pos - myTransform.position).normalized;
float direction = Vector3.Dot(dir, transform.forward);
if(direction > 0.9f){
CC.SimpleMove(myTransform.forward * moveSpeed);
buScript.GoAnimate("walk");
}else
buScript.GoAnimate("idle");
dir = (pos - myTransform.position).normalized;
direction = Vector3.Dot(dir, transform.right);
if(direction > 0.3f) {
_turn = 1;
}
else if(direction < 0.3f) {
_turn = -1;
}
else {
_turn = 0;
}
myTransform.Rotate(0, _turn * Time.deltaTime * rotationSpeed, 0);
}
this is the buScript.around script:
public bool around(Vector3 num, Vector3 around, float prec)
{
bool flag = true;
if(num.x > around.x + prec || num.x < around.x - prec)
flag = false;
if(num.y > around.y + prec || num.y < around.y - prec)
flag = false;
if(num.z > around.z + prec || num.z < around.z - prec)
flag = false;
return flag;
}
and this is what i get: Link
as you can see the rotation is very jumpy if you can help me solve it ill be very happy
Perhaps try rotating a dummy object and use mathf.Clamp
to restrict rotation range, then dump the result into myTransform.rotation
?
can you explain a bit further? its looks like what i did with the "around" is similar to the clamp
Answer by luizgpa · Jan 05, 2012 at 06:44 PM
I guess the problem is the "if-else if-else". _turn will be 0 only when direction is exactly 0.3. If I understood the logic correctly the "else-if" should be else if(direction < -0.3f)
if(direction > 0.3f) {
_turn = 1;
}
else if(direction < -0.3f) {
_turn = -1;
}
else {
_turn = 0;
}
is seems to work but it makes a different bug it seems to prevent them from rotating 180 im using my mouse to tell them where to go if i click behind them they dont move.....
You could rotate using quaternions. Eg:
var targetRotation = Quaternion.LookRotation(dir);
myTransform.rotation = Quaternion.RotateTowards(myTransform.rotation, targetRotation, Time.deltaTime * rotationSpeed);
its even worse now my guys don't move forward and rotating his whole body towards the point
adding the "-" plus a small change fixd it! here the working code
if(direction > 0.05f) { _turn = 1; } else if(direction < -0.05f) { _turn = -1;
} else { _turn = 0; }
Answer by mpavlinsky · Jan 05, 2012 at 07:14 PM
Try using dot product with the forward vector to determine if you need to rotate and then use the right vector to figure out what direction like:
if (Vector3.Dot(dir, transform.forward) < 0.9f) {
if (Vector3.Dot(dir, transform.right) >= 0.0f) {
turn = 1;
} else {
turn = -1;
}
} else {
turn = 0;
}
Also you can consider using the magnitude of the vector between the object and target's positions instead of your around function because it will it will describe radial distance whereas the around function basically does a bounding box test as it is. Unless of course this is your intention.
i triad it like this
dir = (pos - myTransform.position).normalized;
if (Vector3.Dot(dir, transform.forward) < 0.9f) {
if (Vector3.Dot(dir, transform.right) >= 0.0f) {
_turn = 1;
} else {
_turn = -1;
}
} else {
_turn = 0;
}
and the original bug is back... so no good