iwrestledthebeartwice
iwrestledthebeartwice

Reputation: 734

How do I make an object follow a drawn path in Unity?

I wrote a script following some YouTube tutorials and I can finally draw a line with the mouse by holding the mouse button down. While doing this, the points of the line will be stored in a List called fingerPositions. Now, I want to make an object follow this path. How do I achieve it?

Here's the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LineController : MonoBehaviour
{
    public GameObject linePrefab;
    public GameObject currentLine;
    public Rigidbody2D player;
    public float force = 50f;
    public LineRenderer lr;
    public List<Vector2> fingerPositions;

    void Update(){
        if(Input.GetMouseButtonDown(0)){
            createLine();
        }

        if(Input.GetMouseButton(0)){
            Vector2 tempFingerPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            if(Vector2.Distance(tempFingerPos, fingerPositions[fingerPositions.Count-1]) > 0.01f){
                updateLine(tempFingerPos);
            }
        }
        
        if(Input.GetMouseButtonUp(0)){
            followPath();
        }

    }

    void createLine(){
        currentLine = Instantiate(linePrefab, Vector3.zero, Quaternion.identity);
        lr = currentLine.GetComponent<LineRenderer>();
        fingerPositions.Clear();
        fingerPositions.Add(Camera.main.ScreenToWorldPoint(Input.mousePosition));
        fingerPositions.Add(Camera.main.ScreenToWorldPoint(Input.mousePosition));
        lr.SetPosition(0, fingerPositions[0]);
        lr.SetPosition(1, fingerPositions[1]);
    }

    void updateLine(Vector2 newFingerPosition){
        fingerPositions.Add(newFingerPosition);
        lr.positionCount++;
        lr.SetPosition(lr.positionCount-1, newFingerPosition);
    }

    void followPath(){
        for(int i=0; i<fingerPositions.Count; i++){
            player.transform.position = fingerPositions[i];
        }
    }
}

I need to fix the followPath() function and I don't know how to do it. Any help would be appreciated.

Upvotes: 1

Views: 1220

Answers (1)

Kandalf
Kandalf

Reputation: 48

I had an enemy in one of my projects that went back and forth two points, so i think you can use the same logic here.

 //Starting point of your line
public GameObject pointA;

//End point of your line
public GameObject pointB;

//The object you want to move
public GameObject obj;

//Speed the object moves on the line
public int speed;

//Check for which point the object is on
int onA;


void Update()
{

    //Go to point A if you havent been there yet
    if (onA == 0)
    {
        float step = speed * Time.deltaTime;

       
        obj.transform.position = Vector2.MoveTowards(obj.transform.position, pointA.transform.position, step);
        if (obj.transform.position.x == pointA.transform.position.x && obj.transform.position.y == pointA.transform.position.y)
        {
            onA = 1;
        }
    }

    //Go to point B once you have reached point A
    if (onA == 1)
    {
        float step = speed * Time.deltaTime;

        obj.transform.position = Vector2.MoveTowards(obj.transform.position, pointB.transform.position, step);
    }
}

Upvotes: 1

Related Questions