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
2
Question by Arbbot · Jun 10, 2010 at 08:02 PM · physicsaifollowplanetlocomotion

Planet Physics AI Follower

Hi all,

I'm working on a Planet like prove of concept. I'm using Rune's locomotion system Physics Character Controller to move my player and the NPCs. The player has also the Platform Character Controller to capture the input and move the player, but for my NPCs that need to follow my player.

I'm using Rune's AI Follower script but is not working well on the planet surface. I believe it is because it assumes that the NPC is moving on a flat terrain. If anybody can help me updating the script to work on a planet like surface, I'd really appreciate it.

Here's my current script. I'm calling the Follow() and Stop() methods from another script when the player is in range.

using UnityEngine; using System.Collections;

public class FollowController : MonoBehaviour { private CharacterMotor motor;

 // Follow settings
 public float desiredDistance;
 public float walkMultiplier = 0.5f;

 // Use this for initialization
 void Start () {
     motor = GetComponent(typeof(CharacterMotor)) as CharacterMotor;
     if (motor==null) 
         Debug.Log("Follow Controller: Motor is null!!");    
 }

 // Moves the character according to the received target.
 public void Follow (Transform target) 
 {
     // get direction to target
 Vector3 targetVector = target.position-transform.position;
 targetVector = Util.ProjectOntoPlane(targetVector, transform.up).normalized * targetVector.magnitude;
 float speed = (targetVector.magnitude-desiredDistance)*2;

 Vector3 directionVector = targetVector.normalized * speed;

 // Apply direction
 CharacterMotor motor = GetComponent(typeof(CharacterMotor)) as CharacterMotor;
 motor.desiredMovementDirection = directionVector;
 motor.desiredFacingDirection = targetVector;
 }

 public void Stop ()
 {
     motor.desiredMovementDirection = Vector3.zero;  
 }

}

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 Arbbot · Jun 11, 2010 at 12:52 AM 0
Share

Rune, if you are able to check the script and help me out I'd really appreciate it. Basically just need to modify your Follower script to make it work in a Planet like surface. Thanks

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer Wiki

Answer by Arbbot · Jun 11, 2010 at 01:12 AM

Hi,

I just made it work. I needed to make the direction vector relative to the NPC's own orientation and I removed the Util.ProjectOntoPlane() function and it is working pretty well. If you are creating a planet like game, and you are using Rune's locomotion system, this will make your NPC's follow your character around the planet.

Just to clear things out, you don't need to use locomotion (I'm not using locomotion for my NPC's, just for the player). For the NPC's I'm using Rune's PhysicsCharacterMotor.cs (which uses the CharacterMotor.cs). The NPC's also have a CapsuleCollider, a RigidBody, the FollowerController I created based on Rune's Follower script and one extra script to call the Follow() and the Stop() methods of the FollowerController script.

Hope is helpful for other people.

using UnityEngine; using System.Collections;

public class FollowController : MonoBehaviour { private CharacterMotor motor;

 // Follow settings
 public float desiredDistance;
 public float walkMultiplier = 0.5f;

 // Use this for initialization
 void Start () {
     motor = GetComponent(typeof(CharacterMotor)) as CharacterMotor;
     if (motor==null) 
         Debug.Log("Follow Controller: Motor is null!!");    
 }

 // Moves the character according to the received target.
 public void Follow (Transform target) 
 {
     // get direction to target
     Vector3 targetVector = target.position-transform.position;
     float speed = (targetVector.magnitude-desiredDistance)*2;

     Vector3 directionVector = targetVector.normalized * speed;

     // Make direction vector relative to character's own orientation
     directionVector = Quaternion.Inverse (transform.rotation) * directionVector;

     motor.desiredMovementDirection = directionVector;
     motor.desiredFacingDirection = targetVector;
 }

 public void Stop ()
 {
     motor.desiredMovementDirection = Vector3.zero;  
 }

}

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 Bishop · Jul 24, 2011 at 03:39 PM 0
Share

I was working on something not exactly related but your answer actually helped me too. I was trying to send the character to a specific point and it would not go there in a nice way. That was because I forgot to make the direction vector relative to the character orientation too.

avatar image Vampyr_Engel · Jun 09, 2015 at 10:50 AM 0
Share

Sounds useful to me as I wish to make a Battlezone/Imperium Galactica/X-Com Interceptor R.T.S style game with tank warfare on the planets and spaceship fights I got the Faux Gravity and the First Person Controller :Spherical worlds to work but I was thinking about friendly and enemy A.I on spherical worlds and also flying air/spacecraft as well so thanks I will try this out

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

2 People are following this question.

avatar image avatar image

Related Questions

Trying to make a Floating Enemy follow player 1 Answer

Enemy Follow Script Help 2 Answers

Enemy not rotating when inside range 1 Answer

Enemy following Player in range 2 Answers

How to get rid of jumpy collisions? 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