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;  
    }

}

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;  
    }

}