You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
1.3 KiB
C#
53 lines
1.3 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class MoveToBox : MonoBehaviour
|
|
{
|
|
// Start is called before the first frame update
|
|
[SerializeField]
|
|
public GameObject Cube1;
|
|
public Canvas Canva;
|
|
Vector3 init = Vector3.zero;
|
|
Vector3 START;
|
|
float speed = 5;
|
|
void Start()
|
|
{
|
|
START=Cube1.transform.position;
|
|
Canva.enabled=false;
|
|
}
|
|
void Update()
|
|
{
|
|
|
|
if (Input.GetKeyDown("a"))
|
|
{
|
|
Canva.enabled=true;
|
|
StartCoroutine(MoveA(Cube1));
|
|
}
|
|
if(Input.GetKeyDown("b"))
|
|
{
|
|
StartCoroutine(MoveToInit(Cube1));
|
|
}
|
|
if(Cube1.transform.position == START)
|
|
{
|
|
Canva.enabled=false;
|
|
}
|
|
}
|
|
|
|
IEnumerator MoveA(GameObject gameObjectA)
|
|
{
|
|
while (gameObjectA.transform.position != init)
|
|
{
|
|
gameObjectA.transform.position = Vector3.MoveTowards(gameObjectA.transform.position, init,speed * Time.deltaTime);
|
|
yield return null;
|
|
}
|
|
}
|
|
IEnumerator MoveToInit(GameObject gameObjectA)
|
|
{
|
|
while(gameObjectA.transform.position != START) {
|
|
gameObjectA.transform.position = Vector3.MoveTowards(gameObjectA.transform.position, START, speed * Time.deltaTime);
|
|
yield return null;
|
|
}
|
|
}
|
|
}
|