MovingTarget.cs 1.92 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
        if (gameObject.activeSelf){
            PieTarget[] targets = gameObject.transform.parent.GetComponentsInChildren<PieTarget>();
astatham's avatar
astatham committed
16
17
            print(targets.Length);
            for (int i = 0; i < targets.Length; i++)
astatham's avatar
astatham committed
18
            {
astatham's avatar
astatham committed
19
              targets[i].gameObject.SetActive(false);
astatham's avatar
astatham committed
20
                //targets[i].gameObject.transform.GetChild(0).gameObject.SetActive(false);
astatham's avatar
astatham committed
21
22
            }
        }
astatham's avatar
astatham committed
23
        gameObject.SetActive(true);
astatham's avatar
astatham committed
24
        //gameObject.transform.GetChild(0).gameObject.SetActive(true);
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
55
56
57
    }
    // 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);

        }
    }
}