0
\$\begingroup\$

I have a navmesh agent with a trail-renderer component attached to it, who travels to a pre-determined destination based upon user input using unity's built in Navigation system. I want to store the (x,y,z) cordinated of the path traced by the character in some kind of data structure and then I want the camera to trace that exact path, but at some height above so the path(trail-renderer) is visible.

\$\endgroup\$
  • \$\begingroup\$ Why don't you just follow the agent with your camera? Or do you move the camera before or after the agent? \$\endgroup\$ – Rafiwui Aug 24 '17 at 13:02
  • \$\begingroup\$ @Rafiwui I want to move the camera after the agent has reached the destination. The camera should start moving at the same position the character started moving and end up in the same place as the character. Kind of like flashback bt without the character. \$\endgroup\$ – azemda Aug 24 '17 at 13:05
  • \$\begingroup\$ I have sth in mind that you can read the current path of the agent. I will check it as soon as I have access to my unity later this day \$\endgroup\$ – Rafiwui Aug 24 '17 at 13:53
  • \$\begingroup\$ does the path change during the player movement? i.e. is the path recalculated during movement? \$\endgroup\$ – Rafiwui Aug 24 '17 at 16:06
0
\$\begingroup\$

So if the path is static after calculation you could use this:

CameraController.cs:

using UnityEngine;

public class CameraController : MonoBehaviour
{
  public float speed;
  public Vector3 offset;

  Player player;
  int nextWaypoint;

  void Start()
  {
    player = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>();
    nextWaypoint = 0;
  }

  void Update()
  {
    if (player.reachedTarget && nextWaypoint < player.waypoints.Length)
    {
      float step = speed * Time.deltaTime;
      Vector3 target = player.waypoints[nextWaypoint] + offset;

      transform.position = Vector3.MoveTowards(transform.position, target, step);
      if (transform.position == target)
        nextWaypoint++;
    }
  }
}

Player.cs:

using UnityEngine;
using UnityEngine.AI;

public class Player : MonoBehaviour
{
  public Transform target;

  [HideInInspector]
  public Vector3[] waypoints;
  [HideInInspector]
  public bool reachedTarget;

  private NavMeshAgent agent;


  void Start()
  {
    agent = gameObject.GetComponent<NavMeshAgent>();
    StartPathfinding();
    for(int i = 0; i < waypoints.Length; i++)
    {
      Debug.Log(waypoints[i].ToString());
    }
  }

  private void Update()
  {
    if (agent.remainingDistance < 0.01f)
      reachedTarget = true;
  }


  void StartPathfinding()
  {
    agent.SetDestination(target.position);
    NavMeshPath path = new NavMeshPath();
    agent.CalculatePath(target.position, path);
    waypoints = path.corners;
    reachedTarget = false;
  }
}
|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ Thanks for your response, I did not have access to my unity system for the last three days. Will try this solution today. \$\endgroup\$ – azemda Aug 28 '17 at 11:04
  • \$\begingroup\$ @azemda how is it going? \$\endgroup\$ – Rafiwui Aug 30 '17 at 18:00
  • \$\begingroup\$ Good, It worked. I had to change certain things in my script, but it was your concept. Thanks a lot. \$\endgroup\$ – azemda Aug 31 '17 at 10:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.