Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Animate object along a set of xy coordinates?

Discussion in 'Getting Started' started by Ajr_1, Jul 25, 2015.

  1. Ajr_1

    Ajr_1

    Joined:
    May 22, 2013
    Posts:
    20
    Hi, I'm new to Unity so really just looking for a starting point for this. Say I have a set of real-world xy coordinates in a csv file at 1-second intervals. They represent a person's movement in a set space over time. I want to import these and use them to animate an object (eventually a humanoid character, but just a simple sphere/cube to begin with) along the path that the coordinates against time represents. How would I go about doing that?

    First thoughts are:

    1. Would need to translate the real-world coordinates to the Unity Scene
    2. Would need to create a path through all the coordinates
    3. Maybe each coordinate should be a waypoint?
    4. Then animate the object along the path

    Any good tips for how to start this? Thanks in advance!
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    Do you need to make a smooth curve connecting these points? Or is it fine if it just goes in a straight line from point to point?

    In the former case, you probably should just get an existing asset, maybe something like this.

    In the latter case, it's pretty easy to do it yourself. Just keep track of which index you're moving towards, and use code like this in Update:

    Code (CSharp):
    1. transform.position = Vector3.MoveTowards(transform.position, waypoints[nextTargetIdx], speed * Time.deltaTime);
    where speed is a property that controls how fast it moves, and nextTargetIdx is the index into your waypoints array (of type Vector3D). When transform.position == waypoints[nextTargetIdx], increment nextTargetIdx.
     
  3. Ajr_1

    Ajr_1

    Joined:
    May 22, 2013
    Posts:
    20
    Hey, thanks for replying. It would need to make a smooth curve, so I will definitely look into Curvy, thanks for the link.