这个忘了干嘛的
public float speed;
private void Start()
{
Destroy(gameObject, 5);
//p=FindObjectOfType<Player>();
}
void Update () {
if (Player.isAlive )
{
MoveToLeft();
}
}
void MoveToLeft()
{
transform.Translate(Vector2 .left *Time .deltaTime *speed, Space.Self);
创建水管
public GameObject waterPipePrefabs;
float nowtime = 0;
float creattime = 2;
float offY=0f;
//创建水管
void CreatWaterPipes()
{
offY = Random.Range(0,1.6f);
Instantiate(waterPipePrefabs, this.transform.position + new Vector3(0, offY,0), Quaternion.identity);
}
private void Update()
{
if (Player.isAlive == false)
return;
//间隔x秒生产障碍物
nowtime += Time.deltaTime;
if (nowtime >= creattime)
{
CreatWaterPipes();
nowtime = 0;
}
}
}
流程图
小鸟上下跳跃
Rigidbody2D rd2d;
public float upSpeed;
public int playerScore = 0;
public int BestScore = 0;
public Text currentScoreText;
public Text BestScoreText;
void Start () {
Init();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && isAlive )
{
Jump();
}
//当鸟GG的时候,跳转到EndScene
if (isAlive == false)
{
SceneManager.LoadScene("FlappyBird_EndScene");
}
}
void Init()
{
//保证鸟是活的
isAlive = true;
//读取玩家最高得分
BestScore = PlayerPrefs.GetInt("bestScore");
BestScoreText.text = BestScore.ToString();
rd2d = GetComponent<Rigidbody2D>();
playerScore = 0;
//鸟定位到初始位置
transform.position = new Vector3(-1.44f, 0.51f, -0.01f);
}
void Jump()
{
//由于有惯性存在,会导致跳跃力度不稳定
// rd2d.AddForce(new Vector2(0, upPower));
rd2d.velocity = new Vector2(0, upSpeed);
}
//是否活着
public static bool isAlive = true;
void PlayerDead()
{
isAlive = false;
}
private void OnCollisionEnter2D(Collision2D collision)
{
// collision.gameObject.CompareTag("WaterPipe");
if (collision.gameObject .tag .Equals ("WaterPipe" ) )
{
PlayerDead();
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag.Equals("CheckPoint"))
{
//更新得分
playerScore += 1;
//更新最高得分
if(playerScore>BestScore)
{
BestScore = playerScore;
//把得分写入持久数据
PlayerPrefs.SetInt("bestScore", BestScore);
}
//更新UI
currentScoreText.text = playerScore.ToString();
BestScoreText.text = BestScore.ToString();
Debug.Log(playerScore);
}
}
}
背景图移动
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveBg : MonoBehaviour {
// public float scrollSpeed = 0.01f;
//public Renderer rend;
Material mat;
void Start () {
mat = GetComponent<SpriteRenderer>().material;
//rend = GetComponent<Renderer>();
}
// Update is called once per frame
void Update () {
mat.mainTextureOffset = new Vector2( Time.time*0.1f,0);
// float offset = Time.time * scrollSpeed;
//rend.material.SetTextureOffset("_MainTex", new Vector2(offset, 0));
}
}