68 lines
1.7 KiB
C#
68 lines
1.7 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class Game06 : MonoBehaviour{
|
|
|
|
public Transform target;
|
|
public float speed;
|
|
public GameObject death;
|
|
|
|
bool canMove = true;
|
|
Vector3 start, end;
|
|
Rigidbody2D rb2d;
|
|
bool thrown;
|
|
|
|
// Start is called before the first frame update
|
|
void Start(){
|
|
rb2d = GetComponent<Rigidbody2D>();
|
|
rb2d.gravityScale = 0;
|
|
if(target != null){
|
|
target.parent = null;
|
|
start = transform.position;
|
|
end = target.position;
|
|
}
|
|
}
|
|
|
|
void Update(){
|
|
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void FixedUpdate(){
|
|
if(target != null){
|
|
if (canMove){
|
|
float fixedSpeed = speed * Time.fixedDeltaTime;
|
|
transform.position = Vector3.MoveTowards(transform.position, target.position, fixedSpeed);
|
|
}
|
|
}
|
|
|
|
if(transform.position == target.position){
|
|
target.position = (target.position == start) ? end : start;
|
|
}
|
|
if (Input.GetKeyDown(KeyCode.Space)){
|
|
rb2d.gravityScale = 2;
|
|
Invoke("AbleToWin", .5f);
|
|
canMove = false;
|
|
}
|
|
if(rb2d.velocity.y >= -0.1f && thrown && rb2d.velocity.x <= 0.01f){
|
|
FindObjectOfType<GameController>().CompleteLevel();
|
|
}
|
|
}
|
|
|
|
void OnTriggerEnter2D(Collider2D col){
|
|
StartCoroutine(Death());
|
|
death.SetActive(true);
|
|
}
|
|
|
|
IEnumerator Death(){
|
|
yield return new WaitForSecondsRealtime(1f);
|
|
Time.timeScale = 0;
|
|
yield return new WaitForSecondsRealtime(4.5f);
|
|
FindObjectOfType<GameController>().LoseGame();
|
|
}
|
|
|
|
void AbleToWin(){
|
|
thrown = true;
|
|
}
|
|
}
|