版权声明:

  • 本文原创发布于博客园"优梦创客"的博客空间(网址: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);
}
}

游戏改进

1.关卡有点短,后期进行改进
2.后期会多做几个关卡
3.添加其他道具以及特殊场景
4.争取做到模仿的与原版游戏无特别大的差别

Unity经典游戏教程之:冒险岛的更多相关文章

  1. Unity经典游戏教程之:雪人兄弟

    版权声明: 本文原创发布于博客园"优梦创客"的博客空间(网址:http://www.cnblogs.com/raymondking123/)以及微信公众号"优梦创客&qu ...

  2. Unity经典游戏教程之:贪吃蛇

    版权声明: 本文原创发布于博客园"优梦创客"的博客空间(网址:http://www.cnblogs.com/raymondking123/)以及微信公众号"优梦创客&qu ...

  3. Unity经典游戏教程之:是男人就下100层

    版权声明: 本文原创发布于博客园"优梦创客"的博客空间(网址:http://www.cnblogs.com/raymondking123/)以及微信公众号"优梦创客&qu ...

  4. Unity经典游戏教程之:合金弹头

    版权声明: 本文原创发布于博客园"优梦创客"的博客空间(网址:http://www.cnblogs.com/raymondking123/)以及微信公众号"优梦创客&qu ...

  5. Unity经典游戏教程之:弓之骑士

    版权声明: 本文原创发布于博客园"优梦创客"的博客空间(网址:http://www.cnblogs.com/raymondking123/)以及微信公众号"优梦创客&qu ...

  6. Unity经典游戏编程之:球球大作战

    版权声明: 本文原创发布于博客园"优梦创客"的博客空间(网址:http://www.cnblogs.com/raymondking123/)以及微信公众号"优梦创客&qu ...

  7. C#开发Unity游戏教程之Unity中方法的参数

    C#开发Unity游戏教程之Unity中方法的参数 Unity的方法的参数 出现在脚本中的方法,无论是在定义的时候,还是使用的时候,后面都跟着一对括号“( )”,有意义吗?看起来最多也就是起个快速识别 ...

  8. C#开发Unity游戏教程之Scene视图与脚本的使用

    C#开发Unity游戏教程之Scene视图与脚本的使用 Unity中Scene视图的快捷操作 Scene视图是开发者开发游戏时,操作最频繁的视图.因为一旦一个游戏对象被添加到游戏的场景中,就需要首先使 ...

  9. Unity实战案例教程之:不免费的PacMan(初级→中级)

    课程内容介绍: 本套课程适合以下人士: - 免费资料没教会你游戏开发的: - 学了Unity基础不知道怎么用在游戏项目里的: - 想快速开发一款好玩的游戏的: - 想学游戏不知道如何入门的: - 对游 ...

随机推荐

  1. shell遍历文件

    取文件每行的数据,需要按列取  可以 sed 加管道 使用 awk 取列 platform="list.txt" line=`grep -vc '^$' $platform` ; ...

  2. mysql的数据存储

    # pycharm 连接mysql import pymysql username = input("输入用户名:") pwd = input("输入密码:") ...

  3. 字符串和字符编码unicode

    python基础第三天 字符串 str 作用: 用来记录文本(文字)信息,给人类识别用的,为人们提供注释解释说明 表示方式: 在非注释中,凡是用引号括起来的部分都是字符串 ' 单引号 " 双 ...

  4. 配置Windows server 用户和组权限实验详解

    目录 操作步骤如下: 在Windows Server开始菜单下点击管理工具下的计算机管理 新建用户 用户创建完毕 新建文件夹 配置技术部读取"技术资料"和"常用软件&qu ...

  5. C++智能指针的几种用法

    auto在c++11中已经弃用. 一.auto_ptr模板 auto_ptr与shared_ptr.unique_ptr都定义了类似指针的对象,可以将new到的地址赋给这一对象,当智能指针过期时,析构 ...

  6. 使用Mysql执行SQL语句基础操作

    SQL:  又叫结构化语言,是一种用来操作RDBMS的数据库语言,在关系型数据库中都支持使用SQL语句,如oracle.mysql等等. 注意: 在关系型数据库中sql语句是通用的,而在非关系型数据库 ...

  7. 数据结构与算法---线索化二叉树(Threaded BinaryTree)

    先看一个问题 将数列 {1, 3, 6, 8, 10, 14  } 构建成一颗二叉树 问题分析: 当我们对上面的二叉树进行中序遍历时,数列为 {8, 3, 10, 1, 6, 14 } 但是 6, 8 ...

  8. ~~函数基础(二):返回值&作用域~~

    进击のpython 函数的返回值和作用域 上文我们讲到了函数的基础--参数的相关问题 举的例子也都是带有print的函数定义 但是有个问题就出现了:我不想打印这个函数处理后的参数 我想拿到这个参数然后 ...

  9. jekyll搭建个人博客1

    目录 配置环境 使用模板 配置环境 简介 jekyll是一个简单的免费的,生成静态网页的工具,不需要数据库支持.但是可以配合第三方服务,例如Disqus.最关键的是jekyll可以免费部署在Githu ...

  10. WEB前端--返回顶部特效源码

    <!DOCTYPE html><html> <head> <meta charset="utf-8" /> <title> ...