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 LegendSeekerX · Jan 19, 2012 at 02:31 AM · aifollowfollow playersnakefollowing

Following AI - Similar to Snake Game

Hello all,

I am new to Unity (this is my first post) and I am (very sad to admit) quite poor with my math skills. As a programmer, that really isn't a good thing. However, I have come here get some help. What I am trying to do is create a game with just spheres for now. The main player is a sphere, and your friends are spheres. When I "pick up" my friends, I want to them follow directly behind me, but rotate nicely when/if I change angle.

A few problems I have run into, setting the position to the position just behind me works out well enough, except when I change direction, it moves directly behind me which looks silly, also which I haven't a clue in my mathless brain to fix that. I was told Translate isn't great due to "jittery" movement, so I am going to try and avoid that. I have set up rigidbodies on each object so that I can move them via rigidbody transforms, but I haven't really gotten very far. The most common problem I run into is that if I don't just set the friends position directly behind me, and I say velocity +=, the friend eventually "lags" behind because it can't really keep up with the players speed. I've worked on it for a few days and I looked at a few other scripts, and for following AI they work great, but just not for my example.

As additional info, they spheres do not collide with each other, ever, but I'm trying to make it follow directly behind, without passing through the player. Any help would be greatly appreciated, thank you again for reading. :)

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
2
Best Answer

Answer by Owen-Reynolds · Jan 19, 2012 at 03:40 AM

Lots of ways. One cute trick is to have them turn slowly to face the leader, and always move forward. Something like (this isn't as long as it looks):

Transform leader; // set when we're "picked up", to sphere we're behind

// In Update: Vector3 toLeader = leader.position - transform.position;

// Spin to face our leader: Quaternion wantDir = Quaternion.LookRotation( toLeader ); transform.rotation = Quaternion.RotateTowards(transform.rotation, wantDir, 60*Time.deltaTime); // NOTE: the 60 means they turn at 60 degrees/second

// Move: if(toLeader.sqrMagnitude > 2.0f*2.0f) { // stop when we get within 2 rigidBody.velocity = transform.forward * 30; // NOTE: 30 is the speed in meters/sec. // Unity automatically figures out correct amount per frame } else { rigidbody.velocity = Vector3.zero; // prevent coasting // NOTE: w/o this, it will coast to a stop, which could also look nice. }

If you get the move speed and turn radius tweaked correctly, it can make some nice wide turns. If the turn is too small, you get a cool "spiral in" effect.

EDIT (reply to comment):

The angle changes smoothly since, instead of snapping to what we want, we MoveTowards to the goal. The same trick can be used to smoothly change velocity (untested):

Vecor3 wantSpeed;
if(toLeader.sqrMagnitude > 2.0f*2.0f) wantSpeed = transform.forwards * 30.0f;
else wantSpeed = Vector3.zero;
rigidbody.velocity =
  Vector3.MoveTowards(rigidbody.velocity, wantSpeed, 40.0f * Time.deltaTime)_;
  // NOTE: 40.0f is the accelaration in M/S. Can freely change it.
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 LegendSeekerX · Jan 19, 2012 at 06:48 AM 0
Share

Thanks it worked great! I find that the follower is still kind of weird in movement though, it "warps" up behind me, if I move fast enough, he is smooth moving, but at slow speeds it's noticeable.

Thanks again, hope I'm not too demanding, this has just been bothering me the last few days. >_>

avatar image
0

Answer by LegendSeekerX · Jan 23, 2012 at 10:18 PM

Hey Owen, thanks again.

I ended up writing it like this

 Vector3 toLeader = leader.position - transform.position;
         
         //Spin to face leader
         Quaternion wantDir = Quaternion.LookRotation(toLeader);
         transform.rotation = Quaternion.RotateTowards(transform.rotation, wantDir, 360*Time.deltaTime);
         //Note: the 360 means they turn at 360Deg/s
         
         transform.position = leader.transform.position - (toLeader.normalized * 1.5f);

that worked really well for what I was doing.

Thanks again!

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Make NPC follow player when seen? 2 Answers

Having a friendly character follow player 0 Answers

AI Enemy Follow Player 2 Answers

How do I make a enemy follow me 3 Answers

Simple follow script issue 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