- Home /
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;
}
}
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
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;
}
}
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.
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
Follow this Question
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