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