Mina Fawzy
Mina Fawzy

Reputation: 21452

Unity - Draw Path for Characters to follow

I need Instantiate Characters in path moving from the start of path to the end of it.

My tries:

public class EnimaySpwan : MonoBehaviour 
{
    public GameObject gameObject;
    public Transform WaterSpawn; // this where I want my enemy appear 

    private void Update() 
    {
        Instantiate (gameObject, WaterSpawn.position, WaterSpawn.rotation);
    }
}

My object movement script:

public class Mover : MonoBehaviour 
{
    public float speed;

    private void Start() 
    {
        GetComponent<Rigidbody>().velocity = transform.forward * speed;
    }
}

This code makes character move in direct way, not in the specific path I draw as I want.

Problems:

I need to create path for my enemies from start of a path (Instantiation) position to end of path (destination).

How can I approach this, any help?

Upvotes: 3

Views: 18060

Answers (2)

Aaroneiros
Aaroneiros

Reputation: 111

If I understand correctly - you want to spawn an enemy in point A and make it move to point D through points B and C (or any other kind of path). Try placing empty objects on your terrain then:

  1. Spawn enemy
  2. Get transform.position of points B and C in your path.
  3. Keep updating transform.position of the enemy till it gets to B then to C and finally to D. etc

Or you could use a NavMesh. Here is a tutorial explaining how to implement a Nav Mesh Agent: https://unity3d.com/learn/tutorials/modules/beginner/navigation/navmesh-agent

Upvotes: 3

Neven Ignjic
Neven Ignjic

Reputation: 679

Using iTween editor is the easiest way to do this without much programming skill. https://www.assetstore.unity3d.com/en/#!/content/84

Upvotes: 2

Related Questions