-3
\$\begingroup\$
public class MoveTowards : MonoBehaviour {

    int currentStartPoint;
    private Vector3 startMarker, endMarker;
    public float speed = 1.0F;
    private float startTime;
    private float journeyLength;
    public float smooth = 5.0F;
    List<Vector3> tempPositionList = new List<Vector3>();
    List<Vector3> positionList = new List<Vector3>
        {
            new Vector3(4,0,0),
            new Vector3(4,0,2),
            new Vector3(4,0,4),
            new Vector3(2,0,4),
            new Vector3(0,0,4),
            new Vector3(-2,0,4),
            new Vector3(-4,0,4),
            new Vector3(-4,0,2),
            new Vector3(-4,0,0),
            new Vector3(-4,0,-2),
            new Vector3(-4,0,-4),
            new Vector3(-2,-0,-4),
            new Vector3(0,0,-4),
            new Vector3(2,0,-4),
            new Vector3(4,0,-4),
            new Vector3(4,0,-2),
            new Vector3(2,0,-2),
            new Vector3(0,0,-2),
            new Vector3(-2,0,-2),
            new Vector3(-2,0,0),
            new Vector3(-2,0,2),
            new Vector3(0,0,2),
            new Vector3(2,0,2),
            new Vector3(2,0,0),
            new Vector3(0,0,0)
        };
    void Start()
    {
        currentStartPoint = 0;
        SetPoints();
    }
    public void SetPoints()
    {
        startMarker = positionList[currentStartPoint];
        endMarker = positionList[currentStartPoint + 1];
        startTime = Time.time;
        journeyLength = 2;
    }

    void Update()
    {
        Debug.Log(positionList.Count);
        float distCovered = (Time.time - startTime) * speed;
        float fracJourney = distCovered / journeyLength;
        transform.position = Vector3.Lerp(positionList[currentStartPoint], positionList[currentStartPoint + 1], fracJourney);
        if (fracJourney >= 1f && currentStartPoint + 1 < positionList.Count)
        {
            currentStartPoint++;
            SetPoints();
        }
    }

}
\$\endgroup\$
0
0
\$\begingroup\$

There are different ways,

  1. Drag and drop script

    public class YourOtherClass : : MonoBehaviour {
        public MoveTowards moveTowards;//assign by drag and drop in inspector
        void Start () {
            moveTowards.SetPoint();
        }
    }
    
  2. Attach your other script on same Gameobject where your MoveTowards script attached

    public class YourOtherClass : : MonoBehaviour {
        MoveTowards moveTowards;//assign by drag and drop in inspector
    
        void Start () {
            moveTowards = GetComponent<MoveTowards>();
            moveTowards.SetPoint();
        }
    }
    
  3. FindObjectOfType

    void Start () {
        //search by type, it is walid if you have only script with MoveTowards name in scene 
        //hirarcey
        MoveTowards moveTowards = FindObjectOfType<MoveTowards>();
        moveTowards.SetPoint();
    }
    
\$\endgroup\$
7
  • \$\begingroup\$ as you suggested i tried drag and drop getting the following error : error CS1061: Type MoveTowards' does not contain a definition for index' and no extension method index' of type MoveTowards' could be found. Are you missing an assembly reference? \$\endgroup\$ – Nihal Saxena Oct 4 '17 at 6:06
  • \$\begingroup\$ This error occurs when you try to call a method or access a class member that does not exist. \$\endgroup\$ – Muhammad Faizan Khan Oct 4 '17 at 6:12
  • \$\begingroup\$ can you share complete error. \$\endgroup\$ – Muhammad Faizan Khan Oct 4 '17 at 6:13
  • \$\begingroup\$ Assets/Swipe.cs(111,29): error CS1061: Type MoveTowards' does not contain a definition for index' and no extension method index' of type MoveTowards' could be found. Are you missing an assembly reference? \$\endgroup\$ – Nihal Saxena Oct 4 '17 at 6:14
  • \$\begingroup\$ that error was by my mistake but after adding script also my code is not working \$\endgroup\$ – Nihal Saxena Oct 4 '17 at 6:18

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