GameObject move to each Vector3s in a list
Hey :)
I'm trying to get a gameObject to move to a start position then move through a list of Vector3s before reaching the end of a path. I've got the start and end positions working well, however, I do not know how to get a gameObject to move through the Vector3 list INORDER. I've tried getting the positionList[i] to convert to a position, however, nothing seems to be working and I'm not sure how to write a sequence to move to each individual point.
This is the code I'm working with;
if (!startPositionIsSet && middlePossitionListIsSet && endPositionIsSet && Mathf.Approximately(gameObject.transform.position.magnitude, startPoint.magnitude))
{
for (int i = 0; i < positionList.Count; i++)
{
gameObject.transform.position = Vector3.Lerp(gameObject.transform.position, positionList[i], 1 / (duration * (Vector3.Distance(gameObject.transform.position, positionList[i]))));
Debug.Log("I am at " + transform.position + " Moving to position " + positionList[i]);
Answer by Cuttlas-U · Apr 07, 2017 at 09:03 AM
hi;
u should use Coroutine for this I write the code for u ;
just put the vector3 in the inspector and set the object that u want to move between positions;
public Vector3[] positions;
public Transform ObjectToMove;
public float MoveSpeed = 8;
Coroutine MoveIE;
void Start()
{
StartCoroutine(moveObject());
}
IEnumerator moveObject()
{
for (int i = 0; i < positions.Length; i++)
{
MoveIE = StartCoroutine(Moving(i));
yield return MoveIE;
}
}
IEnumerator Moving(int currentPosition)
{
while (ObjectToMove.transform.position != positions[currentPosition])
{
ObjectToMove.transform.position = Vector3.MoveTowards(ObjectToMove.transform.position, positions[currentPosition] , MoveSpeed * Time.deltaTime);
yield return null;
}
}
Your answer
Follow this Question
Related Questions
Helicopter Move Local Position,Local Position 1 Answer
Are I stupid? Vector3 1 Answer
Reversing movement directions 1 Answer
List.Contains not working as intended 0 Answers
Trying to transform my character's local position on a GetKey. 0 Answers