• 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 venkspower · Dec 01, 2011 at 10:41 AM · animationfollowwaypoint

An object to follow another object through the waypoint - problem: taking a shortest path instead of walking through the waypoint.

Hello, I have created an object which is moving good along the waypoints. This is the first part. And I have introduced another object. And have calculated the distance between the second object and all other waypoints.

My objective is to move the first object to the waypoints, which are near to the second object. Which is to be done dynamically. And I have taken the waypoints in an array, initially. My code goes like this:-

 var waypoint : Transform[];
 
 var dist1 =  Vector3.Distance(waypoint[1].position,object2.transform.position);
 var dist2 =  Vector3.Distance(waypoint[2].position,object2.transform.position);
 var dist3 =  Vector3.Distance(waypoint[3].position,object2.transform.position);
 var dist4 =  Vector3.Distance(waypoint[4].position,object2.transform.position);
 var dist5 =  Vector3.Distance(waypoint[5].position,object2.transform.position);
 var dist6 =  Vector3.Distance(waypoint[6].position,object2.transform.position);

 var minimumDistance = Mathf.Min(dist1,dist2,dist3,dist4,dist5,dist6);
 if(minimumDistance==dist1)
 {
     object1.transform.position = waypoint[1].position;
             object1.animation.Play("walk");
 }
 if(minimumDistance==dist2)
 {
     object1.transform.position = waypoint[2].position;
             object1.animation.Play("walk");
 }
 if(minimumDistance==dist3)
 {
     girl.transform.position = waypoint[3].position;
             object1.animation.Play("walk");
 }
 if(minimumDistance==dist4)
 {
     object1.transform.position = waypoint[4].position;
             object1.animation.Play("walk");
 }
 if(minimumDistance==dist5)
 {
     object1.transform.position = waypoint[5].position;
             object1.animation.Play("walk");
 }
 if(minimumDistance==dist6)
 {
     object1.transform.position = waypoint[6].position;
             object1.animation.Play("walk");
 }

I am sorry. This is trivial, I believe. But I am not able to do animation. But I have mentioned it in the script. But why it isn't happening, I don't know. Help! Thanks in advance.

Comment
Add comment · Show 13
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 syclamoth · Dec 01, 2011 at 11:05 AM 0
Share

Does this happen in Update? Because that would mean you are instantly teleporting to positions, and restarting the animation every frame.

avatar image venkspower · Dec 01, 2011 at 12:10 PM 0
Share

I apologize. I did not mention. Yes, it happens in Update().

avatar image syclamoth · Dec 01, 2011 at 09:21 PM 0
Share

Then you have a problem. You should only be calling this code exactly when you want your object to move- what happens here is the animation gets restarted every frame, and never gets a chance to play! I've posted a cleaner version of your current code below, but it will still have the same problem if you use it in the same way.

Could you explain what you are trying to do here a little better? I'm still not quite sure what the point of this code is, since it looks like it teleports 'object1', meaning a walk animation isn't appropriate anyway!

avatar image venkspower · Dec 02, 2011 at 04:19 AM 0
Share

Alright. See, I am building a car showroom, where the object2(customer)(3rd person controller) arrives at the car. I have given waypoints around the car, so that the object1(salesgirl) moves with the object2, but through the waypoints only.

Here I am calculating the $$anonymous$$imum distance between object2 and the waypoints. The object1(salesgirl) should walk to the nearest waypoint to object2(customer). This is my objective.

avatar image venkspower · Dec 02, 2011 at 05:03 AM 0
Share

Even though I have given,

if(condition) { velocity = moveDirection.normalized * speed; girl.animation.Play("walk"); }

else { velocity = Vector3.zero; girl.animation.Play("idle"); }

It is not walking! And, yes, I have given this in Update().

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by syclamoth · Dec 01, 2011 at 11:12 AM

Well, I have no idea how to fix your actual problem, since you haven't given me enough details, but that code needs to be a lot cleaner.

 var waypoints : Transform[];

 var closestWaypoint : Transform;
 var closestDistance : float = 9999999;
 for(var waypoint : Transform in waypoints)
 {
     // Check every waypoint
     var curDist : float = Vector3.Distance(waypoint.position, object2.transform.position);
     if(curDist < closestDistance)
     {
         // This is the closet one yet!
         closestDistance = curDist;
         closestWaypoint = waypoint;
     }
 }
 object1.transform.position = closestWaypoint.position;
 object1.animation.Play("walk");


Much nicer! Also, you can use this with any number of waypoints without having to add extra lines.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

GameObject follow animation 1 Answer

How to make characters follow a path? 2 Answers

Need help figuring out why my animated object stops at a waypoint after rotation. 0 Answers

Constant total time over multiple Lerp functions 4 Answers

Animation Stops Unexpected. (Script included) 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