Y
Y
yraiv2022-01-19 12:09:07
Unity
yraiv, 2022-01-19 12:09:07

How to pass through given points in unity3d one by one?

I made a small code, I want the character to move from point to point. I ran into a problem, if I have only 1 point, then everything is clear, the character goes to it and everything is fine, but I must have an array and the character must move to them in turn, but I don’t understand how to implement it. You can check for each point manually, but I understand that this is stupid and there should be an option that will do this for all points in a row by brute force.

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

public class ZombieStandart : MonoBehavior
{

public float speed = 1f;
public int health;
public int attack;
public Transform[] WayPoints;

public void FixedUpdate()
{
transform.position = Vector3.Lerp(transform.position,WayPoints[].position, Time.deltaTime * speed); // swears that [] should contain a value, if the array is removed here and in the declaration, then it works, but it goes only to 1 given point.
}

}


It seems to me that it would be more correct to check if the object reached the coordinates of the first point, then the second point would be substituted and it would already go to it, etc. but I don't understand how to do it

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nekit Medvedev, 2022-01-20
@NIKROTOS

An iterator pattern to the rescue.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question