Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class move : MonoBehaviour
- {
- public bool canMove;
- public bool canMoveRight;
- public bool canMoveLeft;
- public bool canMoveToZeroRight;
- public bool canMoveToZeroLeft;
- public int linePos;
- public int speed = 5;
- private void Start()
- {
- canMoveToZeroLeft = false;
- canMoveToZeroRight = false;
- canMove = false;
- canMoveRight = false;
- canMoveLeft = false;
- linePos = 0;
- }
- void Update()
- {
- transform.Translate(Vector3.forward * speed * Time.deltaTime);
- if (Input.GetKeyDown(KeyCode.D)) { TestMove(Vector3.right); }
- else if (Input.GetKeyDown(KeyCode.A)) { TestMove(Vector3.left); }
- else { TestMove(Vector3.zero); }
- }
- public void TestMove(Vector3 dir)
- {
- if (linePos == 0 & !canMove)
- {
- canMoveToZeroRight = false;
- canMoveToZeroLeft = false;
- canMoveRight = true;
- canMoveLeft = true;
- canMove = false;
- }
- else if (linePos > 0 & ! canMove)
- {
- canMoveToZeroRight = false;
- canMoveToZeroLeft = true;
- canMoveRight = false;
- canMoveLeft = false;
- canMove = false;
- }
- else if (linePos < 0 & !canMove)
- {
- canMoveToZeroRight = true;
- canMoveToZeroLeft = false;
- canMoveRight = false;
- canMoveLeft = false;
- canMove = false;
- }
- if (dir == Vector3.right)
- {
- Debug.Log("Right");
- canMoveLeft = false;
- canMove = true;
- }
- else if (dir == Vector3.left)
- {
- Debug.Log("Left");
- canMoveRight = false;
- canMove = true;
- }
- if (canMoveRight & canMove)
- {
- float x = transform.position.x;
- x = Mathf.Lerp(transform.position.x, 1.7f, 0.05f);
- transform.position = new Vector3(x, transform.position.y, transform.position.z);
- if (transform.position.x >= 1.6f)
- {
- Debug.Log("false1");
- canMove = false;
- linePos++;
- }
- }
- if (canMoveLeft & canMove)
- {
- float x = transform.position.x;
- x = Mathf.Lerp(transform.position.x,-1.7f, 0.05f);
- transform.position = new Vector3(x, transform.position.y, transform.position.z);
- if (transform.position.x <= -1.6f)
- {
- Debug.Log("false2");
- canMove = false;
- linePos--;
- }
- }
- if (canMoveToZeroRight & canMove || canMoveToZeroLeft & canMove)
- {
- float x = transform.position.x;
- x = Mathf.Lerp(transform.position.x, 0f, 0.05f);
- transform.position = new Vector3(x, transform.position.y, transform.position.z);
- if (transform.position.x >= -0.1f & transform.position.x <= 0.1f)
- {
- Debug.Log("false2");
- canMove = false;
- if(linePos > 0) { linePos--; }
- if (linePos < 0) { linePos++; }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement