View difference between Paste ID: 52kXGvSv and 8hgZYySM
SHOW: | | - or go back to the newest paste.
1
using System.Collections;
2
using System.Collections.Generic;
3
using UnityEngine;
4
5
public class move : MonoBehaviour
6
{
7
    public bool canMove;
8
    public bool canMoveRight;
9
    public bool canMoveLeft;
10
    public bool canMoveToZeroRight;
11
    public bool canMoveToZeroLeft;
12
    public int linePos;
13
    public int speed = 5;
14
    private void Start()
15
    {
16
        canMoveToZeroLeft = false;
17
        canMoveToZeroRight = false;
18
        canMove = false;
19
        canMoveRight = false;
20
        canMoveLeft = false;
21
        linePos = 0;
22
    }
23
    void Update()
24
    {
25
26
27
        transform.Translate(Vector3.forward * speed * Time.deltaTime);
28
        if (Input.GetKeyDown(KeyCode.D)) { TestMove(Vector3.right); }
29
        else if (Input.GetKeyDown(KeyCode.A)) { TestMove(Vector3.left); }
30
        else { TestMove(Vector3.zero); }
31
    }
32
33
34
35
36
    public void TestMove(Vector3 dir)
37
    {
38
       
39
        if (linePos == 0 & !canMove)
40
        {
41
            canMoveToZeroRight = false;
42
            canMoveToZeroLeft = false;
43
            canMoveRight = true;
44
            canMoveLeft = true;
45
            canMove = false;
46
        }
47
        else if (linePos > 0 & ! canMove)
48
        {
49
            canMoveToZeroRight = false;
50
            canMoveToZeroLeft = true;
51
            canMoveRight = false;
52
            canMoveLeft = false;
53
            canMove = false;
54
        }
55
        else if (linePos < 0 & !canMove)
56
        {
57
            canMoveToZeroRight = true;
58
            canMoveToZeroLeft = false;
59
            canMoveRight = false;
60
            canMoveLeft = false;
61
            canMove = false;
62
        }
63
64
        if (dir == Vector3.right)
65
        {
66
            Debug.Log("Right");
67
            canMoveLeft = false;
68
            canMove = true;
69
70
        }
71
        else if (dir == Vector3.left)
72
        {
73
            Debug.Log("Left");
74
            canMoveRight = false;
75
            canMove = true;
76
77
        }
78
        
79
        if (canMoveRight & canMove)
80
        {
81
            float x = transform.position.x;
82
            x = Mathf.Lerp(transform.position.x, 1.7f, 0.05f);
83
            transform.position = new Vector3(x, transform.position.y, transform.position.z);
84
            if (transform.position.x >= 1.6f)
85
            {
86
                Debug.Log("false1");
87
                canMove = false;
88
                linePos++;
89
90
            }
91
        }
92
        if (canMoveLeft & canMove)
93
        {
94
            float x = transform.position.x;
95
            x = Mathf.Lerp(transform.position.x,-1.7f, 0.05f);
96
          transform.position = new Vector3(x, transform.position.y, transform.position.z);
97
            if (transform.position.x <= -1.6f)
98
            {
99
                Debug.Log("false2");
100
                canMove = false;
101
                linePos--;
102
            }
103
        }
104
        if (canMoveToZeroRight & canMove || canMoveToZeroLeft & canMove)
105
        {
106
            float x = transform.position.x;
107
            x = Mathf.Lerp(transform.position.x, 0f, 0.05f);
108
            transform.position = new Vector3(x, transform.position.y, transform.position.z);
109
            if (transform.position.x >= -0.1f & transform.position.x <= 0.1f)
110
            {
111
                Debug.Log("false2");
112
                canMove = false;
113
                if(linePos > 0) { linePos--; }
114
                if (linePos < 0) { linePos++; }
115
            }
116
        }
117
118
    }
119
120
  
121
122
}