x


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

}
more ▼

asked Jun 10 '10 at 08:02 PM

Arbbot gravatar image

Arbbot
90 5 5 11

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

Jun 11 '10 at 12:52 AM Arbbot
(comments are locked)
10|3000 characters needed characters left

1 answer: sort newest

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

}
more ▼

answered Jun 11 '10 at 01:12 AM

Arbbot gravatar image

Arbbot
90 5 5 11

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.

Jul 24 '11 at 03:39 PM Bishop
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1948
x982
x319
x98
x69

asked: Jun 10 '10 at 08:02 PM

Seen: 2108 times

Last Updated: Jul 24 '11 at 03:39 PM