unity——小球酷跑游戏制作

这篇具有很好参考价值的文章主要介绍了unity——小球酷跑游戏制作。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

课堂课程记录——小球滚动

所有变量与物体名的命名原则都是见名知意

一、创建一个unity项目
二、Create所需3Dobject
1.Player
unity——小球酷跑游戏制作2.walls
unity——小球酷跑游戏制作
三、添加属性
1.添加在Player上
a.添加Rigidbody组件unity——小球酷跑游戏制作
b.添加new script组件,并命名为PlayMove,代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class playMove : MonoBehaviour
{
    public Rigidbody rd;
    public float speadAutoMove=5;
    public float speadMoveUpandDown=20;
    // Start is called before the first frame update
    void Start()
    {
        rd=gameObject.GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void Update()
    {
        PlayerAutoMove();
        PlayerMoveUpandDown();
    }
    private void PlayerAutoMove(){
        rd.AddForce(Vector3.right*speadAutoMove);   //前进
    }
    private void PlayerMoveUpandDown()
    {
        float v=Input.GetAxis("Vertical");  //上下
        rd.AddForce(v*Vector3.up*speadMoveUpandDown);//给一个上下的力量

    }
}

2.添加到walls上
a.首先create empty将wall包含
unity——小球酷跑游戏制作
b.在Wall上添加new script组件,代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class wallControl : MonoBehaviour
{
    private float offset;
    public GameObject player;
    // Start is called before the first frame update
    void Start()
    {
        offset=gameObject.transform.position.x-player.transform.position.x;
    }

    // Update is called once per frame
    void Update()
    {
        FollowPlayMove();
    }
    void FollowPlayMove(){
        gameObject.transform.position=new Vector3(player.transform.position.x+offset,0,0);
    }
}

3.实现相机跟随
a.在相机上添加new script 组件并命名为cameraControl,代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class cameraControl : MonoBehaviour
{
    public GameObject player;
    private float offset_camera;
    // Start is called before the first frame update
    void Start()
    {
        offset_camera=gameObject.transform.position.x-player.transform.position.x;
    }

    // Update is called once per frame
    void Update()
    {
        FollowCameraMove();
    }
    void FollowCameraMove(){
        gameObject.transform.position=new Vector3(offset_camera+player.transform.position.x,gameObject.transform.position.y,gameObject.transform.position.z);
    }
}

b.将script中设置的player变量赋值:
unity——小球酷跑游戏制作
至此基本的小球滚动游戏就完成了。
unity——小球酷跑游戏制作

继续上节课的内容:

4.将player的形状改为球形
左键选中player的属性:unity——小球酷跑游戏制作
将mesh属性由cube改为sphere
5.创建障碍预制体
a.先创建一个3D物体cube,将其命名为barrier。
b.在project的asset中创建prefab预制体文件
unity——小球酷跑游戏制作
并将之前创建的barrier直接拖拽到prefab中。
若对prefab预制体的作用不理解的话,可访问如下链接:
预制体的制作与功能
若barrier物体变为蓝色,则创建成功。
unity——小球酷跑游戏制作
6.随机生成障碍物
a.创建一个空物体,然后命名为barrierControl。
unity——小球酷跑游戏制作
b.在该物体上添加new script的组件,C#代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BarrierControl : MonoBehaviour {
    public int barrierInterval=5;
    public GameObject player;
    public GameObject CurrentBarrier;
    public GameObject BarrierPre;
	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
        AutoCreatBarrier();

    }
    // 障碍物自动生成
    public void AutoCreatBarrier()
    {
        if(player.transform.position.x>CurrentBarrier.transform.position.x)
        {
            //生成新的障碍物
            float targetX = CurrentBarrier.transform.position.x + barrierInterval;
            float targetY = RandomBarrierPosition();
            Vector3 targetPos = new Vector3(targetX,targetY,0);
            GameObject g = Instantiate(BarrierPre,targetPos,Quaternion.identity);
            //随机大小
           g.transform.localScale = new Vector3(g.transform.localScale.x, RandomBarrierSize((int)g.transform.position.y), g.transform.localScale.z);
            //判断障碍更换
            CurrentBarrier = g;
        }
    }
    //障碍随机大小
    public float RandomBarrierSize(int r)
    {
        int rAbs = Mathf.Abs(r);
        if(rAbs==0)
        {
            return 6;
        }
        else
        {
            return (3-rAbs)*2+1;
        }
    }
    //障碍物随机位置
    public float RandomBarrierPosition()
    {
       int r = Random.Range(-3,3);
        Debug.Log(r);
        return r;
    }

}

到目前为止障碍物就能不断的在与小球的距离控制下产生了。
7.障碍的清除
a.在之前的wall文件夹中创建一个新cube物体,命名为trigger,控制大小长度在略小于上下wall之间,以便过滤掉与其接触的barrier(切记不要接触上下的wall,否则游戏一开始就会将上下的wall给消除,小球一下就掉下去了)。
unity——小球酷跑游戏制作
b.右键选中trigger,在右侧的属性栏,移除Cube(Mesh Filter)和Mesh Renderer属性。
unity——小球酷跑游戏制作
使trigger成为透明状态:
unity——小球酷跑游戏制作

c.给trigger添加new script组件,C#代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AutoDestoryBarriers : MonoBehaviour {

    private void OnTriggerEnter(Collider other)
    {
        Destroy(other.gameObject);
    }
}

8.给障碍物添加随机产生颜色功能

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BarrierColor : MonoBehaviour {
    public Material[] barrierMaterial;
	// Use this for initialization
	void Start () {
        int i = Random.Range(0,barrierMaterial.Length);
        gameObject.GetComponent<Renderer>().material = barrierMaterial[i];
	}
	
	// Update is called once per frame
	void Update () {
		
	}
}

unity——小球酷跑游戏制作

9.碰到障碍物颜色提示
unity——小球酷跑游戏制作
C#代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerColorControl : MonoBehaviour
{
        private void OnCollisionEnter(Collision collision)
        {
           // Debug.Log("1");
           //分数减少
           UIcontrol._instance.AddScore(-10);
            gameObject.GetComponent<Renderer>().material.color=Color.red;
        }
        private void OnCollisionExit(Collision collision)
        {
            gameObject.GetComponent<Renderer>().material.color=Color.white;
        }
}

9.分数记录
unity——小球酷跑游戏制作
C#代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 using UnityEngine.UI;

public class UIcontrol : MonoBehaviour
{
   public Text scoreText;
   public int score=0;


    //单列模式
    public static UIcontrol _instance;
    private void Awake()
    {
        _instance=this;
    }
    public void AddScore(int x)
    {
        score+=x;
        scoreText.text="得分:"+score;
    }
}

并在barrierControl中添加如下代码进行分数增加
unity——小球酷跑游戏制作
在Playcolorcontrol中添加分数减少代码:
unity——小球酷跑游戏制作
完整画面如下:
unity——小球酷跑游戏制作

以上小球酷跑课程内容就结束了,稍后我会对游戏进行进一步的完善功能。文章来源地址https://www.toymoban.com/news/detail-423062.html

到了这里,关于unity——小球酷跑游戏制作的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Unity学习记录:制作双屏垃圾分类小游戏

    要实现的功能 游戏操作 在规定时间内,垃圾通过拖拽进入正确垃圾桶的容器,垃圾在这里消失,飞入第二个屏上对应垃圾桶的位置并实现加分和加时间的效果,垃圾拖拽进入不正确的垃圾桶,垃圾会返回到原来的位置,同时,相应的时间也会减少 胜利和失败的条件: 胜利:

    2024年02月03日
    浏览(37)
  • 【Unity】小球吃方砖小游戏

    目录 游戏背景 游戏开发         2.1场景布置         2.2小球运动         2.3镜头跟踪         2.4吃掉方砖         2.5结束提示 游戏录制           用wasd(↑←↓→)操控小球进行平面滑动,小球触碰会原地打转的立方体后立方体会消失,消除5个小球后提示

    2024年02月09日
    浏览(30)
  • unity小球吃金币小游戏

    链接放在这里 unity小球吃金币小游戏-Unity3D文档类资源-CSDN下载 这是我在学完虚拟现实技术这门课程后利用unity所做的小球吃金币小游戏,里面有源码和作品源文件,用u更多下载资源、学习资料请访问CSDN下载频道. https://download.csdn.net/download/m0_57324918/85604051 1创建Roll A Ball小球吃

    2023年04月08日
    浏览(33)
  • Unity和C#游戏编程入门:创建迷宫小球游戏示例

    💂 个人网站:【工具大全】【游戏大全】【神级源码资源网】 🤟 前端学习课程:👉【28个案例趣学前端】【400个JS面试题】 💅 寻找学习交流、摸鱼划水的小伙伴,请点击【摸鱼学习交流群】 当涉及到Unity和C#游戏编程入门时,以下是一些示例代码,可以帮助初学者更好地理

    2024年02月08日
    浏览(31)
  • c++编写天天酷跑游戏

    素材加Q群:723550115 Start importing material (background picture) Create a graph window and define macros for the window Import game background (scroll cycle) Local modularization Game background coordinates ​ The picture is the Y coordinate of motion, and the definition amount is constantly changed to keep the last y coordinate change initialization

    2024年02月16日
    浏览(35)
  • Unity制作虚拟主机装机模拟器(课程设计)

    1.设计阶段 1.1需求分析 本虚拟装机系统是为了帮助用户学习计算机的组装过程,提供动手组装、教学模式和零件介绍三种模式。在零件介绍中,用户可以通过语音和文字介绍了解不同电脑零件的功能和名称。在教学模式中,用户可以观看动画短片,了解计算机的发展和组装计

    2024年01月21日
    浏览(33)
  • 简单的天天酷跑小游戏实现

    2024年02月02日
    浏览(28)
  • 【unity】快速了解游戏制作流程-制作九宫格简单游戏demo

            hi~大家好呀!欢迎来到我的unity学习笔记系列~,本篇我会简单的记录一下游戏流程并且简单上手一个通过九宫格移动到指定位置的小游戏,话不多说,我们直接开始吧~                  本篇源自我看B站一位up主的视频所做的笔记,感兴趣的可以去看原视频哦

    2023年04月08日
    浏览(43)
  • qt-有趣的小球游戏大球吃小球

    https://download.csdn.net/download/u013083044/88861691?spm=1001.2014.3001.5503

    2024年02月22日
    浏览(28)
  • python制作跳跃的小球

    11.1.安装pygame库 pip install pygame 11.2.加载模块初始化 11.3.创建窗口 作用:创建游戏窗口 常见的内置方法: 方法 作用 pygame.dispaly. init() 初始化display pygame.dispaly. quit() 结束display模块 pygame.dispaly. get_init() 判断是否初始化 pygame.dispaly. get.surface() 获取当前surface对象 pygame.dispaly. flip

    2024年02月03日
    浏览(27)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包