using UnityEngine;
public class AnimationBG : MonoBehaviour
{
Material material;
Vector2 movement;
public Vector2 speed;
void Start()
{
material = GetComponent<Renderer>().material;
}
void Update()
{
movement += speed * Time.deltaTime;
material.mainTextureOffset = movement;
}
}
2.FanPlatform
using UnityEngine;
public class FanPlatform : MonoBehaviour
{
Animator animator;
void Start()
{
animator = GetComponent<Animator>();
}
void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.CompareTag("Player"))
{
animator.Play("Fan_run");
}
}
}
3.GameManager
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class GameManager : MonoBehaviour
{
static GameManager instance;
public Text timeScore;
public GameObject gameOverUI;
void Awake()
{
if (instance != null)
{
Destroy(this);
}
instance = this;
instance.gameOverUI.SetActive(false);
}
void Update()
{
//timeScore.text = Time.time.ToString("00");
//每一个场景加载的时候,重新计算时间
timeScore.text = Time.timeSinceLevelLoad.ToString("00");
}
public void RestartGame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
Time.timeScale = 1f;
}
public void Quit()
{
Application.Quit();
}
public static void GameOver(bool dead)
{
if (dead)
{
instance.gameOverUI.SetActive(true);
Time.timeScale = 0f;
}
}
}
4.LineRender
using UnityEngine;
public class LineRender : MonoBehaviour
{
LineRenderer line;
public Transform startPoint;
public Transform endPoint;
void Start()
{
line = GetComponent<LineRenderer>();
}
void Update()
{
line.SetPosition(0, startPoint.position);
line.SetPosition(1, endPoint.position);
}
}
5.Platform
using UnityEngine;
public class Platform : MonoBehaviour
{
GameObject topLine;
Vector3 movement;
public float speed;
void Start()
{
movement.y = speed;
topLine = GameObject.Find("TopLine");
}
void Update()
{
MovePlatform();
}
void MovePlatform()
{
transform.position += movement * Time.deltaTime;
if (transform.position.y >= topLine.transform.position.y)
{
Destroy(gameObject);
}
}
}
6.PlayerController
using UnityEngine;
public class PlayerController : MonoBehaviour
{
Rigidbody2D rb;
Animator anim;
public float speed;
public bool isOnGround;
public float checkRadius;
public LayerMask platform;
public GameObject groundCheck;
float xVelocity;
void Start()
{
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
void Update()
{
isOnGround = Physics2D.OverlapCircle(groundCheck.transform.position, checkRadius, platform);
//掉落的动画
anim.SetBool("isOnGround", isOnGround);
Movement();
}
void Movement()
{
//没有触摸
if (Input.touchCount <= 0)
{
return;
}
//单点触摸, 水平上下移动
if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved)
{
//EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId)为true表示点到了UI
// if (!UnityEngine.EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
// {
xVelocity = Input.GetTouch(0).deltaPosition.x;
/* if (deltaposition > 0)
{
xVelocity = 1;
}
else if (deltaposition < 0)
{
xVelocity = -1;
}
else {
xVelocity = 0;
}*/
// transform.Translate(-deltaposition.x * 0.1f, 0f, -deltaposition.y * 0.1f);
// }
}
//返回-1,0,1
//xVelocity = Input.GetAxisRaw("Horizontal");
rb.velocity = new Vector2(xVelocity * speed, rb.velocity.y);
//跑到的动画
anim.SetFloat("speed", Mathf.Abs(rb.velocity.x));
if (xVelocity != 0)
{
if (xVelocity > 0)
{
xVelocity = 1;
}
else if (xVelocity < 0)
{
xVelocity = -1;
}
else
{
xVelocity = 0;
}
transform.localScale = new Vector3(xVelocity, 1, 1);
}
}
bool playerDead;
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("Spike"))
{
anim.SetTrigger("dead");
}
}
void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.CompareTag("Fan"))
{
rb.velocity = new Vector2(rb.velocity.x, 6f);
}
}
public void PlayerDead()
{
playerDead = true;
Destroy(gameObject);
GameManager.GameOver(playerDead);
}
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.blue;
Gizmos.DrawWireSphere(groundCheck.transform.position, checkRadius);
}
}
7.Spawner
using System.Collections.Generic;
using UnityEngine;
public class Spawner : MonoBehaviour
{
public List<GameObject> platforms = new List<GameObject>();
public float spawnTime;
private float countTime;
private Vector3 spawnPosition;
void Update()
{
SpawnPlatform();
}
public void SpawnPlatform()
{
countTime += Time.deltaTime;
spawnPosition = transform.position;
spawnPosition.x = Random.Range(-3.5f, 3.5f);
if (countTime >= spawnTime)
{
CreatePlatform();
countTime = 0;
}
}
public void CreatePlatform()
{
int index = Random.Range(0, platforms.Count);
int spikeNum = 0;
if (index == 4)
{
spikeNum++;
}
if (spikeNum > 1)
{
spikeNum = 0;
countTime = spawnTime;
return;
}
GameObject newPlatform = Instantiate(platforms[index], spawnPosition, Quaternion.identity);
newPlatform.transform.SetParent(this.gameObject.transform);
}
}
Speed Down1.AnimationBGusing UnityEngine;public class AnimationBG : MonoBehaviour{ Material material; Vector2 movement; public Vector2 speed; void Start() { material = GetComponent<Renderer>().material; } vo