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 · Dec 08, 2011 at 01:44 PM · rotationmovementgameobjectvector3character

GameObject wont stop rotating ?

Hey, I created a code to make my GameObjects move to where i click with my mouse.

this.pos is the final position.

CC is the CharacterControl.

I used the around method so the GameObject will stop around the final position because there is no chance that he will get to the exact position.

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

  void Update () {
     if(!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);
 
         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);
     }
 }

The problem is that when the unit reaches around the final pos, he keeps rotating a little left a little right but without moving so it will never reach the desired pos and will never stop rotating..

Comment
Add comment · Show 3
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 Anonymous 1 · Dec 08, 2011 at 01:53 PM 0
Share

yup that interesting... so you think it will never stop rotating because it will never literally get to exact position ?

avatar image dorpeleg · Dec 08, 2011 at 02:07 PM 0
Share

Yes, i mean it always gets inside the if(!around... because it never gets around that point..

avatar image Justin Warner · Dec 08, 2011 at 02:17 PM 0
Share

In an else ($$anonymous$$eaning it is around the object, yeah?) you could remove this script.... Destroy(this);. Or no? Lol.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by ks13 · Dec 08, 2011 at 02:52 PM

Actually, why not use Vector3.MoveTowards(currentPosition, targetPosition, distance); If you make the distance a percentage of the total distance you won't have to worry about never attaining the targeted position.

You should get something like :

void Update () {
    if(myTransform.position != this.pos)
    {
      percentageDone += step;
      myTransform.position = Vector3.MoveTowars(beginingPosition, this.pos, Vector3.Distance(beginingPosition, this.pos) * percentageDone);
      myTransform.Rotate(0, Time.deltaTime * rotationSpeed, 0);
    }
}

Where percentageDone is the % of distance already traveled by the object, and step the % you want your object to move every frame.

Comment
Add comment · 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

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

6 People are following this question.

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

Related Questions

Movement relative to Camera and XZ Plane 1 Answer

Character MoveLook rotation locked? 1 Answer

Setting Up Vector and Still Rotating the Character 1 Answer

Character Controller Movement - Different speeds on different axis 1 Answer

Rotate the camera around a gameObject with a dynamic up value. 0 Answers


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