• 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 eXtremeTY · Jan 20, 2013 at 05:50 PM · rotationmovementpositiontargetunit

How to stop a unit from rotation once it gets to designated position?

Hello guys, I'm having troubles.. With the script I have so far, when the unit reaches its designated position, it still keeps rotation itself standstill..

T$$anonymous$$s is the code:

 if (target != t$$anonymous$$s.transform.position)
         {
             // find the vector pointing from our poisiton to the target
             directionToMove = (target - t$$anonymous$$s.transform.position).normalized;
             // create the rotation we need to be in to look at the target
             lookRotation = Quaternion.LookRotation(directionToMove);
             // rotate us over time
             t$$anonymous$$s.transform.rotation = Quaternion.Slerp(t$$anonymous$$s.transform.rotation, lookRotation, Time.deltaTime * rotationSpeed);
             t$$anonymous$$s.transform.position += t$$anonymous$$s.transform.forward * moveSpeed * Time.deltaTime;
             
             isMoving = true;
         }

Any ideas? Thanks!

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

2 Replies

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

Answer by Griffo · Jan 20, 2013 at 07:33 PM

Adapt t$$anonymous$$s -

 #pragma strict
 
     var distance : float;
     var target : Transform;    
     var lookAtDistance : int = 15.0;
     var attackRange : int = 10.0;
     var standOffDistance : float = 2;
     var moveSpeed : int = 5.0;
     var damping : int = 6.0;
 
     function Update () 
     {
     distance = Vector3.Distance(target.position, transform.position);
 
     if(distance < lookAtDistance)
     {
     lookAt ();
     }   
     if((distance < attackRange) && (distance > standOffDistance))
     {
     attack ();
     }
 }
 
 
 function lookAt ()
 {
 var rotation = Quaternion.LookRotation(target.position - transform.position);
 transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
 }
 
 function attack ()
 {
     transform.Translate(Vector3.forward * moveSpeed *Time.deltaTime);
 }
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 eXtremeTY · Jan 20, 2013 at 08:34 PM 0
Share

Thank you, actually both of your works have helped me, and I managed to combine it together and get a working result! Thanks!

avatar image
1

Answer by sparkzbarca · Jan 20, 2013 at 06:20 PM

 lookRotation = Quanternion.LookRotation(directionToMove - transform.forward)
 
 mark as answered and have a nice day :)
Comment
Add comment · Show 3 · 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 eXtremeTY · Jan 20, 2013 at 07:19 PM 0
Share

Not so fast cowboy :D I'd be more then happy to just copy-n-paste it, and mark your post as answered and go on having fun :)

But, it won't work.. With your code attached (modified), the only difference is that tanks moves in a jerkish fashion toward position, and once it gets there, he starts rotating itself once again :(

avatar image sparkzbarca · Jan 20, 2013 at 07:41 PM 2
Share

wait are you saying it keeps rotating always or it rotates once you you arrive at your destination?

I think you mean once you arrive you rotate in circles.

Thats because it never reaches its destination perfectly and thats fine. It's hard to hit something to within millionths of a meter.

undo earlier and keep lookrotation (directionToMove)

change

if(target != transform.position)

to

if(vector3.distance(target,transform.position) > .01f) or any really small number really.

Basically you just want to stop rotating if the distance between the taret and the position is sufficiently small.

mathf.epsilon produces a REALLY REALLY small number

like .1 * - 10^42

but that might be too fine. that distance is in meters

.02 meters = 1 inch approx

so .01 is actually less than half an inch its .4 inches or so

you could probably set it even as high as .05 and that'd stop rotating within about 2 to 2 1/2 inches.

so yea just

 if(vector3.distance(target,transform.position) > acceptable_distance_in_meters)
avatar image eXtremeTY · Jan 20, 2013 at 08:34 PM 0
Share

Thank you, actually both of your works have helped me, and I managed to combine it together and get a working result! Thanks!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 People are following this question.

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

Related Questions

Tilt with rotation. 0 Answers

how to move an object, but make it stay on the ground 1 Answer

make player move in direction it's facing 2 Answers

Objects not always Spawning At Correct Location 2 Answers

Setting z value of trans.pos relative to rotation 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