x


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

more ▼

asked Jan 04 '12 at 07:43 PM

dorpeleg gravatar image

dorpeleg
914 8 17 26

Perhaps try rotating a dummy object and use mathf.Clamp to restrict rotation range, then dump the result into myTransform.rotation?

Jan 05 '12 at 08:07 AM asafsitner

can you explain a bit further? its looks like what i did with the "around" is similar to the clamp

Jan 05 '12 at 06:21 PM dorpeleg
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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;
   }     
more ▼

answered Jan 05 '12 at 06:44 PM

luizgpa gravatar image

luizgpa
866 4 9

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.....

Jan 05 '12 at 06:56 PM dorpeleg

You could rotate using quaternions. Eg:

var targetRotation = Quaternion.LookRotation(dir);

myTransform.rotation = Quaternion.RotateTowards(myTransform.rotation, targetRotation, Time.deltaTime * rotationSpeed);

Jan 05 '12 at 07:34 PM luizgpa

its even worse now my guys don't move forward and rotating his whole body towards the point

Jan 05 '12 at 08:11 PM dorpeleg

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; }

Jan 07 '12 at 05:26 PM dorpeleg
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Jan 05 '12 at 07:14 PM

mpavlinsky gravatar image

mpavlinsky
581 5 5 11

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

Jan 05 '12 at 08:05 PM dorpeleg
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2244
x2171
x747
x599
x59

asked: Jan 04 '12 at 07:43 PM

Seen: 567 times

Last Updated: Jan 07 '12 at 05:27 PM