MovingTarget.cs 1.75 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MovingTarget : MonoBehaviour {

    public float posChangeSpeed, maxPos, minPos;
    public static float currentPos = 8f;
    public static float previousPos = 8f;

    void Start()
    {
            previousPos = -9f;
astatham's avatar
astatham committed
14
15
16
17
        if (gameObject.activeSelf){
            PieTarget[] targets = gameObject.transform.parent.GetComponentsInChildren<PieTarget>();
            for (int i = 0; i < gameObject.transform.parent.childCount; i++)
            {
astatham's avatar
astatham committed
18
              targets[i].gameObject.SetActive(false);
astatham's avatar
astatham committed
19
20
            }
        }
astatham's avatar
astatham committed
21
        gameObject.SetActive(true);
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
    }
    // Update is called once per frame
    void Update()
    {
        if (currentPos >= minPos && currentPos < maxPos && currentPos > previousPos)
        {
            gameObject.transform.position = new Vector3(currentPos, -3.65f, 0);
            previousPos = currentPos;
            currentPos += (posChangeSpeed * Time.deltaTime);
        }
        else if (currentPos <= maxPos && currentPos > minPos && currentPos < previousPos)
        {
            gameObject.transform.position = new Vector3(currentPos, -3.65f, 0);
            previousPos = currentPos;
            currentPos -= (posChangeSpeed * Time.deltaTime);

        }
        else if (currentPos > previousPos)
        {
            gameObject.transform.position = new Vector3(currentPos, -3.65f, 0);
            previousPos = currentPos;
            currentPos -= (posChangeSpeed * Time.deltaTime);

        }
        else if (currentPos < previousPos)
        {
            gameObject.transform.position = new Vector3(currentPos, -3.65f, 0);
            previousPos = currentPos;
            currentPos += (posChangeSpeed * Time.deltaTime);

        }
    }
}