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

This is the code:

 if (target != this.transform.position)
         {
             // find the vector pointing from our poisiton to the target
             directionToMove = (target - this.transform.position).normalized;
             // create the rotation we need to be in to look at the target
             lookRotation = Quaternion.LookRotation(directionToMove);
             // rotate us over time
             this.transform.rotation = Quaternion.Slerp(this.transform.rotation, lookRotation, Time.deltaTime * rotationSpeed);
             this.transform.position += this.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 this -

 #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 (directionTo$$anonymous$$ove)

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.

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

Interpolating the motion of a camera moving from one node to another 1 Answer

How to make camera move backwards in relation to player (so behind player), regardless of y rotation of player and camera 1 Answer

Determining the rotation of an object based on its upcoming position? 1 Answer

Tilt with rotation. 0 Answers

how to move an object, but make it stay on the ground 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