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 MouseCaneta · Mar 20, 2012 at 10:08 PM · followsmooth

Problems with follow script (pet like)

Hi people.

I'm trying to make a simple script to make an object follow another object smoothly. I've seen some examples here in other answers, but in all of them I'm having the same problem (including my solution which is below). My problem is, the object do follow the target object though when it reaches the minimum distance it starts to move very very slowly to a random direction or in a circle trajectory around the target object. Can anyone tell me what am I doing wrong?

 using UnityEngine;

using System.Collections;

public class AllyFollow : MonoBehaviour {

 public Transform target;
 public float smoothTime = 0.3f;
 public float distance = 2.5f;
 public float correction = 0.1f;
 private Vector2 velocity;
 private Vector3 thisPosition;
 private Transform thisTransform;
 
 void Start ()
 {
     thisTransform = transform;
 }
 
 void Update ()
 {
     // Look at target
     Vector3 targetRelativeDirection = target.position - thisTransform.position;
     targetRelativeDirection.y = 0;
     
     thisTransform.forward = targetRelativeDirection;
     
     // Seems to work fine up to here...
     
     Debug.Log("value 1 - " + targetRelativeDirection.magnitude);
     Debug.Log("value 2 - " + (distance + correction));
     
     // Follow target if distance between me and target is 
     if (targetRelativeDirection.magnitude > distance + correction)
     {
         Debug.Log("FOLLOWING");
         
         thisPosition = thisTransform.position;
         
         thisPosition.x = Mathf.SmoothDamp(thisTransform.position.x, (target.position.x - (distance * targetRelativeDirection.normalized.x)), ref velocity.x, smoothTime);
         thisPosition.z = Mathf.SmoothDamp(thisTransform.position.z, (target.position.z - (distance * targetRelativeDirection.normalized.z)), ref velocity.y, smoothTime);
         
         thisTransform.position = thisPosition;
     }
     else
     {
         Debug.Log("NOT FOLLOWING");
     }
 }

}

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 MouseCaneta · Mar 20, 2012 at 10:56 PM 0
Share

I figured it out. I shouldn't be moving this object by changing its transform properties because it won't respect any colliders or physics calculations. This movement problem I was having was due to the physics being applied to an object that was having it's transform component changed directly. This was conflicting with the physics calculations.

What I recommend for people trying to do this or other similar things is to never move an object changing it's transform properties unless you want to warp it into a place. If you want movement to feel natural like a character following another character, add a character controller or some other physics component to the object and move them using the methods of these components, like CharacterController.Simple$$anonymous$$ove.

Thanks anyway!

2 Replies

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

Answer by MouseCaneta · Mar 20, 2012 at 11:12 PM

I figured it out. I shouldn't be moving this object by changing its transform properties because it won't respect any colliders or physics calculations. This movement problem I was having was due to the physics being applied to an object that was having it's transform component changed directly. This was conflicting with the physics calculations.

What I recommend for people trying to do this or other similar things is to never move an object changing it's transform properties unless you want to warp it into a place. If you want movement to feel natural like a character following another character, add a character controller or some other physics component to the object and move them using the methods of these components, like CharacterController.SimpleMove.

Thanks anyway!

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
avatar image
0

Answer by DaveA · Mar 20, 2012 at 10:23 PM

I've seen this before. What I do is check if the distance is small (like .01) and set a boolean 'moving' to false. So as soon as it gets in that radius, it stops. And you need to check against a somewhat larger distance to start again, like .015 or something. Hysteresis I think it's called.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Smooth Camera Follow Script, Weird Movement... Please help! 1 Answer

Unity 2D Object Smooth Follow Problem C# 2 Answers

Timer activates script 1 Answer

smooth follow only on X and Y 1 Answer

SmoothFollow Unity Script improvements? 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