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();
}
}
}
\$\begingroup\$
\$\endgroup\$
0
Add a comment
|
1 Answer
\$\begingroup\$
\$\endgroup\$
7
There are different ways,
Drag and drop script
public class YourOtherClass : : MonoBehaviour { public MoveTowards moveTowards;//assign by drag and drop in inspector void Start () { moveTowards.SetPoint(); } }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(); } }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(); }
-
\$\begingroup\$ as you suggested i tried drag and drop getting the following error : error CS1061: Type
MoveTowards' does not contain a definition forindex' and no extension methodindex' of typeMoveTowards' could be found. Are you missing an assembly reference? \$\endgroup\$Nihal Saxena– Nihal Saxena2017-10-04 06:06:43 +00:00Commented Oct 4, 2017 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– Muhammad Faizan Khan2017-10-04 06:12:46 +00:00Commented Oct 4, 2017 at 6:12
-
\$\begingroup\$ can you share complete error. \$\endgroup\$Muhammad Faizan Khan– Muhammad Faizan Khan2017-10-04 06:13:26 +00:00Commented Oct 4, 2017 at 6:13
-
\$\begingroup\$ Assets/Swipe.cs(111,29): error CS1061: Type
MoveTowards' does not contain a definition forindex' and no extension methodindex' of typeMoveTowards' could be found. Are you missing an assembly reference? \$\endgroup\$Nihal Saxena– Nihal Saxena2017-10-04 06:14:24 +00:00Commented Oct 4, 2017 at 6:14 -
\$\begingroup\$ that error was by my mistake but after adding script also my code is not working \$\endgroup\$Nihal Saxena– Nihal Saxena2017-10-04 06:18:50 +00:00Commented Oct 4, 2017 at 6:18