Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by dorpeleg · Jan 04, 2012 at 07:43 PM · rotationgameobjectvector3rotaterotating

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image asafsitner · Jan 05, 2012 at 08:07 AM 0
Share

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

avatar image dorpeleg · Jan 05, 2012 at 06:21 PM 0
Share

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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;
    }     
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dorpeleg · Jan 05, 2012 at 06:56 PM 0
Share

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

avatar image luizgpa · Jan 05, 2012 at 07:34 PM 0
Share

You could rotate using quaternions. Eg:

var targetRotation = Quaternion.LookRotation(dir);

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

avatar image dorpeleg · Jan 05, 2012 at 08:11 PM 0
Share

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

avatar image dorpeleg · Jan 07, 2012 at 05:26 PM 0
Share

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

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dorpeleg · Jan 05, 2012 at 08:05 PM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Rotate a gameobject like car tire 2 Answers

Rotate "ghost object" then instantiate object with that rotation 1 Answer

managing rotation of Turret on two axis 1 Answer

How Rotate Object To Resemble Spinning? 1 Answer

Rotation for 180 degrees of character 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges