For a little mini game, I want to move a sprite(bug) from flower to flower in a bug like fashion.
Like this image:[49262-bug-mover.jpg*|49262]
but I have just had so many errors, and so may issues that I just had to ask how someone would do it. Here is my entire code:
[SerializeField] private Transform bug;
[SerializeField] private Transform flowers;
private Transform[] targets;
[SerializeField] private AnimationCurve tween;
public float WaitAtFlowerTime = 2f;
public int currentFlowerID = 0;
public float reachDistance = 350;
private float startingDistance;
public bool drawGizmos = false;
public Vector3 GetNodePos(int id)
{
return targets[id].localPosition;
}
private Vector3 orig;
public float Speed;
private void Start()
{
targets = new Transform[flowers.transform.childCount];
for(int i = 0; i < flowers.transform.childCount; i++)
{
targets *= flowers.transform.GetChild(i).transform;*
}
orig = bug.transform.localPosition;
}
private void Update()
{
Vector3 dest = GetNodePos(currentFlowerID);
Vector3 offset = dest - bug.transform.localPosition;
if (offset.sqrMagnitude > reachDistance)
{
offset = offset.normalized;
bug.transform.Translate(offset * Speed * Time.deltaTime, Space.World);
bug.transform.localPosition = new Vector3(bug.transform.localPosition.x, new AnimationCurve(new Keyframe(0, orig.y), new Keyframe(0.5f, (offset.y + 70f)), new Keyframe(1, orig.y)).Evaluate(Speed * Time.deltaTime), bug.transform.localPosition.z);
}
else
{
ChangeDestFlower();
}
}
private void ChangeDestFlower()
{
orig = targets[currentFlowerID].position;
currentFlowerID++;
if (currentFlowerID >= targets.Length)
{
currentFlowerID = 0;
}
}
The current error I have makes the bug fly around like mad, and then hover on the Y axis at 0 directly above a flower.
Anyone have any ideas?
*