版權聲明:
- 本文原創發布於博客園"優夢創客"的博客空間(網址:
http://www.cnblogs.com/raymondking123/
)以及微信公眾號"優夢創客"(微信號:unitymaker) - 您可以自由轉載,但必須加入完整的版權聲明!
游戲簡介
是一款動作冒險類游戲,由HUDSON公司發售,是一款2D橫向卷軸游戲。游戲的主人公是當時游戲少年們所稱贊的高橋名人與冒險島中的名人很像。
場景搭建
1.將3張背景連接
前景層
1.將玩家,障礙,怪物,道具與勝利點放置在背景上
2.設置他們的Sorting Layer為ForeGround
3.按照他們的先后設置他們的Order in Layer 的大小
攝像機位置的限制
1.在玩家子節點下建立一個新的坐標點用來控制偏移量
···
public Transform player, boundLeft, boundRight;//創建公有的坐標點
Vector3 pos;
void Start () {
pos = transform.position - player.position;
}
void Update () {
Vector3 camPos = transform.position;//新建一個攝像機坐標點
camPos.x =player.position.x+ pos.x;
if(camPos.x>=boundRight.position.x)
{
return;
}
//camPos.x = Mathf.Clamp(player.position.x, boundLeft.position.x, boundRight.position.x);//限制攝像機的X軸在min 和max之間
camPos.y = this.transform.position.y;
camPos.z = this.transform.position.z;//-10
this.transform.position = camPos;
}
2.在設置一個最右坐標點,防止攝像機跑到地圖外面去
給背景添加一個音樂播放器
子彈的發射
1.設置一個子彈預制體控制傷害以及碰撞判定消除
public float damage = 10;
public void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "boss")
{
collision.gameObject.GetComponent<Enemy>().Hit(damage);
}
if (collision.gameObject.tag == "kulou")
{
collision.gameObject.GetComponent<Enemy1>().Hit(damage);
}
if (collision.gameObject.tag != "Player")
{
Destroy(this.gameObject);
}
}
2.將子彈的Rigidbody 2D中的Gravity Scale設置為3,這樣可以保證子彈發出去以后會有一個下落
3.在人物身上添加一個子節點坐標點用來控制子彈的發射位置
4.給這個位置寫一個腳本用來控制子彈的速度以及顯示
public float speed = 10f;
public GameObject rocketPrefab;
void Start()
{
}
public void Shoot()
{
var rocket = Instantiate(rocketPrefab);
rocket.transform.position = this.transform.position;
if (this.transform.parent.localScale.x > 0)
{
rocket.transform.rotation = Quaternion.Euler(0, 0, 0);
rocket.GetComponent<Rigidbody2D>().velocity = new Vector2(speed, 0);
}
else
{
rocket.transform.rotation = Quaternion.Euler(0, 0, 180);
rocket.GetComponent<Rigidbody2D>().velocity = new Vector2(-speed, 0);
}
}
人物控制
1.添加動畫控制器
2.給人物添加一個腳本用來更新角色的行動並將攻擊作為一個bool值,一開始為false,當吃到道具時再開啟
public float moveSpeed = 10f;
public float moveForce = 10f;
private bool jump = false;//是否按了跳躍鍵並且可以跳躍
public float jumpForce = 255f;
public bool gongji = false;
public static playerControl instance;//單件模式
private Rigidbody2D rb;
void Start()
{
rb = this.GetComponent<Rigidbody2D>();
}
public void Awake()
{
instance = this;
}
void Update()
{
RaycastHit2D hit = Physics2D.Raycast(this.transform.position, Vector2.down, 0.5f, 1 << LayerMask.NameToLayer("Ground"));
Debug.DrawRay(this.transform.position, Vector2.down);
if (Input.GetButtonDown("Jump") && hit)
{
jump = true;
}
if (Input.GetButtonDown("Fire1"))
{
if (gongji == true)
{
GetComponent<Animator>().SetTrigger("Fire");
}
}
}
void FixedUpdate()
{
if (PlayerHealth.instance.gameOver == 0)
{
float h = Input.GetAxis("Horizontal");
Vector2 force = new Vector2(h * moveForce, 0);
if (Mathf.Abs(rb.velocity.x) < moveSpeed)
{
GetComponent<Animator>().SetFloat("Speed", h);
rb.AddForce(force);
}
if (Mathf.Abs(rb.velocity.x) >= moveSpeed)
{
rb.velocity = new Vector2(Mathf.Sign(rb.velocity.x) * moveSpeed, rb.velocity.y);
}
if (h > 0)
{
var s = this.transform.localScale;
s.x = Mathf.Abs(s.x);
this.transform.localScale = s;
}
else if (h < 0)
{
var s = this.transform.localScale;
s.x = -Mathf.Abs(s.x);
this.transform.localScale = s;
}
if (jump)
{
GetComponent<Animator>().SetTrigger("Jump");
rb.AddForce(Vector2.up * jumpForce);
jump = false;
}
}
}
3.在人物上添加一個腳本,用來控制玩家是否死亡以及碰撞
public int gameOver;
void Update()
{
TimeOut();
if (gameOver ==1)
{
this.GetComponent<Animator>().SetTrigger("dead");
if (Input.GetMouseButtonDown(0))
{
Application.LoadLevel(Application.loadedLevelName);
}
}
if (gameOver == 2)
{
this.GetComponent<Animator>().SetTrigger("Firedead");
if (Input.GetMouseButtonDown(0))
{
Application.LoadLevel(Application.loadedLevelName);
}
}
}
public void OnCollisionEnter2D(Collision2D collision)
{
if(collision.gameObject.tag=="Wall")
{
this.GetComponent<Animator>().SetTrigger("sleep");
}
if(collision.gameObject.tag=="boss")
{
this.gameOver = 1;
}
if(collision.gameObject.tag=="Fire")
{
this.GetComponent<Animator>().SetTrigger("Firedead");
this.gameOver = 2;
}
}
private void TimeOut()
{
if (timeLeft > 0)
{
timeLeft = timeLeft - Time.deltaTime;
timetext.text = "Time Left:" + (int)timeLeft;
}
if (timeLeft > 50)
{
timeLeft = 50;
}
if (timeLeft <= 0)
{
timeLeft = 0f;
this.gameOver = 1;
}
}
public void Playerdead()
{
gameOver = 1;
Vector2 v = new Vector2(0, 5);
this.GetComponent<Rigidbody2D>().velocity = v;
this.GetComponent<BoxCollider2D>().isTrigger = true;
}
public void PlayerFireDead()
{
gameOver = 2;
this.GetComponent<BoxCollider2D>().isTrigger = true;
}
4.在這個腳本上控制道具的得分以及時間加成
private int score = 0;
public Text scoreText;
public float timeLeft = 20;
public Text timetext;
public void Score()
{
if (this.gameOver == 1)
return;
score += 100;
scoreText.text = "Score:" + score;
timeLeft += 3;
}
public void Duyao()
{
if (this.gameOver == 1)
return;
timeLeft -=7;
}
public void Niunai()
{
if (this.gameOver == 1)
return;
score += 300;
scoreText.text = "Score:" + score;
}
public void Timeup()
{
if (this.gameOver == 1)
return;
timeLeft += 10;
}
public void yaoshi()
{
if (this.gameOver == 1)
return;
score += 500;
scoreText.text = "Score:" + score;
timeLeft += 10;
}
怪物的移動與碰撞
1.給怪物添加一個碰撞器
2.給牆添加一個碰撞器和一個標簽為Wall,使得怪物碰撞到Wall會進行一個反向移動的效果
void FixedUpdate()
{
RaycastHit2D hit = Physics2D.Linecast(FrontCheck.position,this.transform.position);
if(hit&&hit.transform.gameObject.tag=="Wall")
{
Vector3 s = this.transform.localScale;
s.x = -s.x;
this.transform.localScale = s;
}
Rigidbody2D rb = GetComponent<Rigidbody2D>();
Vector2 v = new Vector2(this.transform.localScale.x * speed, rb.velocity.y);
rb.velocity = v;
}
3.以怪物自身中心為起點在怪物的加點下面添加一個子節點為射線的終點
private Transform FrontCheck;
void Start () {
FrontCheck = transform.Find("frontCheck");
}
3.在怪物腳本里給怪物設置血量,每次碰到子彈減去相對的血量,當血量沒有時觸發死亡效果
public float hp = 1;
public float damage = 1;
public void Hit(float damage)
{
hp -= damage;
if (hp <= 0)
{
if (dead != null)
{
this.GetComponent<SpriteRenderer>().sprite = dead;
foreach (var c in GetComponents<Collider2D>())
{
c.isTrigger = true;
}
GetComponent<Rigidbody2D>().freezeRotation = false;
}
}
}
游戲勝利
1.將游戲勝利人物設置為一個觸發器IsTrigger
2.設置腳本,當人物觸發勝利人物時,在屏幕中央跳出游戲勝利的字幕
重新開始
在人物死亡與游戲勝利的時候,點擊鼠標左鍵,就可以重新開始
public GameObject WinText;
public void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.name == "player")
{
WinText.SetActive(true);
}
if (Input.GetMouseButtonDown(0))
{
Application.LoadLevel(Application.loadedLevelName);
}
}