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 Rukey4 · Mar 29, 2015 at 05:51 PM · javascriptaifollow

Simple follow script issue

Hello,

I've been trying this script I've only slightly modified for my use. I just want my object to follow my player, and stop when in range. Now it all works fine when I don't have a Rigidbody on my following object. It stops fine but the object slowly floats and transforms to what seems to be the middle of my player character.

To counteract this I added a rigidbody, it jitters along the floor (As I imagine because it's trying to float whilst the gravity from the ridigbody is trying to pull it down) When it stops, it's very jittery.

I've tried to disable the rigidbody whilst not moving, but still has an issue.

Could someone offer any guidance on what I could try? Thanks!

 var target : Transform;
 
 var rotationSpeed = 3.0;
 var moveSpeed = 3.0;
 var maxAttackDistance = 0.0;
 
 var isFree : boolean = true;
 
 private var myPosition : Transform;
 
 function Awake()
 {
     myPosition = transform;
 }
 
 function Start()
 {
     var go = GameObject.FindGameObjectWithTag("Player");
     target = go.transform;
 }
 
 function Update()
 {
     if(isFree == true)
     {
         PlayerDetected();
     }
     
     else
     {
         animation.Play("Idle");
     }
 }
 
 function PlayerDetected()
 {
     myPosition.rotation = Quaternion.Slerp(myPosition.rotation, Quaternion.LookRotation(target.position - myPosition.position), rotationSpeed * Time.deltaTime);
     
     if(Vector3.Distance(target.position, myPosition.position) >= maxAttackDistance)
     {
         myPosition.position += transform.forward * moveSpeed * Time.deltaTime;
         animation.Play("Run");
     }
     
     else 
     {
         animation.Play("Idle");
     }
 }
 
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Socrates · Mar 30, 2015 at 08:44 AM

On line 39, you are looking at the distance between the target's position and this object's position. Then you move toward the target's position as long as that distance is greater than or equal to 0.0 (see line 5). That is causing your problem.

You do not want to match the target's position exactly. You want to move to the target's position less an offset. You have to leave room for size of both the target and the object.

Think of it this way: Pretend both target and object are spheres. You want to move them next to each other. If you get to where Vector3.Distance(target,object) is less than the radius size of the target plus the radius size of the object, the two spheres have collided and are partially inside each other.

So, you need to make sure your maxAttackDistance is set to be large enough to reflect how "thick" the object and the target are. If you will want to add in some extra distance to this if you do not want the object to touch your target at all.

If the object has any issues with the vertical motion after the buffer is in place, you may want to take a look at computing the Distance only in X and Z, then adjusting the Y separately to get the object to be hovering at the correct height. (i.e. If you want the object to float at the target's head height, not at the target's center point.)

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 Rukey4 · Apr 02, 2015 at 06:01 PM 0
Share

That was a good explanation, I'm curious whether there is a way to force the Y value to be frozen?

I guess as a work around I could add an empty gameobject to the height of which I want to AI to stay (Attached to my player) as the target.

avatar image Socrates · Apr 03, 2015 at 09:50 PM 0
Share

If you are using a rigidbody, you can lock the position or rotation on the three axes in the Inspector.

But you're probably not going to use a rigidbody for this. I was mostly thinking of what you were saying that when you did use a rigidbody, you were getting vertical jittering along the floor.

As a simple example of moving just X and Z:

 // Assume that computed$$anonymous$$ove is a Vector3 you've put together already, but which as Y move too.
 Vector3 limited$$anonymous$$ovement = new Vector3(computed$$anonymous$$ove.x, 0f, computed$$anonymous$$ove.z);
 myPosition.position += limited$$anonymous$$ovement * moveSpeed * Time.deltaTime;
 

avatar image Rukey4 · Apr 04, 2015 at 10:02 AM 0
Share

I think that makes sense.

I modified it slightly because it looked a little like C#.

 var computed$$anonymous$$ove : Vector3;
 
 var limited$$anonymous$$ovement : Vector3 = new Vector3(computed$$anonymous$$ove.x, 0f, computed$$anonymous$$ove.z);
 myPosition.position +=  limited$$anonymous$$ovement * moveSpeed * Time.deltaTime;

How do I make it translate forward? If I'm not using "transform.forward"? I understand that we need to use computed$$anonymous$$ove but how do we use it?

Thanks again!

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

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

Related Questions

Enemy following Player in range 2 Answers

Help with Enemy AI 1 Answer

How I can make a simple follow AI in Unity 5? 1 Answer

Enemy not rotating when inside range 1 Answer

I am making an AI Script and i need to find out how to make the enemy move in random x and z axis movements(Random!!!)This script works, but the movement is never ending. 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