• 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 Xatoku · Mar 28, 2011 at 05:53 AM · objectfollow

"Pet Like" Follow

Would somebody either explain how I'd go about it or tell me how to have an object follow another object and stop once within a certain distance? Imagine a pet following a character, something basic like that.

Comment
Add comment · Show 1
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 Meater6 · Mar 28, 2011 at 12:53 PM 0
Share

Do you need a exact tested script? Or just some help writing it? If you just need some help I suggest for you to just edit the script below from Justin Warner.

1 Reply

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

Answer by Justin Warner · Mar 28, 2011 at 11:47 AM

var minDist; var speed;

function Update(){ chase(); } function chase(){ //Find which way player is facing, make the "pet" point to it and move at speed*1 checkDistance(); } function checkDistance(){ //var distance = distance between player and pet; if(distance <= minDist){ speed = 0; } else{ speed = //Whatever your speed is... } }

Something like this should work =).

Comment
Add comment · Show 2 · 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 Xatoku · Mar 28, 2011 at 03:45 PM 0
Share

Perfect answer cuz now I get to test myself hehe. So for the first part I would rotate the Pet until a Raycast hits the Player, translate the pet forward, and then go to the check distance function and say if the Pet is like >5 units away from the Player then stop chasing and make speed 0, and if <5 units from him continue chasing and make speed like 3 or something.

$$anonymous$$y next question is, should I do a isChaseable variable and then move the checkdistance to the update function? And say like if the distance is ever greater than 5, isChaseable = true?

avatar image Justin Warner · Mar 28, 2011 at 08:57 PM 0
Share

You can, however, the if statement in the checkDistance does that, but you can always make it use a real boolean if you'd like. It really is how you like to code. But yes, you have the basic idea =). Just keep tweaking things, you'll get it eventually =). Good luck!

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

1 Person is following this question.

avatar image

Related Questions

How to follow a moving object by camera on a line ? 1 Answer

Object where the mouse is 4 Answers

2D how to make a sprite follow the mouse position #c 2 Answers

Getting an Object to follow a Sine Curve 0 Answers

Make instantiated object follow player or another object 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