【unity游戏】小球酷跑小游戏开发

news/2024/7/8 6:38:14

前言

想必很多人小时候都喜欢玩天天酷跑,这个游戏也是陪伴了很多人的童年,今天我们用unity做一个简易的小游戏《小球酷跑》。
游戏效果图:
在这里插入图片描述

一、游戏玩法?

由玩家操控一个小球,来躲避前面的障碍物,碰到障碍物则游戏失败。

二、开发过程

首先是游戏场景布置,来hierarchy页面鼠标右键,new–>3d object–>sphere,重命名为player,继续new–>3d object–>cube两次,分别为墙体上下两部分,命名为wallUp和wallDown,这里我设置的wall的xyz参数分别是:20,1,2。
在这里插入图片描述
在小球上添加物理属性,在inspector页面add Component选择physics里的Rigidbody。

通过代码控制小球移动,代码如下:

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

public class playMove : MonoBehaviour {
    public Rigidbody rd;
    public float speedAutoMove = 5;
    public float speedMoveUpandDown = 20;
	void Start () {
        rd = gameObject.GetComponent<Rigidbody>();
	}
	
	void Update () {
        PlayerAutoMove();
        PlayerMoveUpandDown();

    }
    private void PlayerAutoMove()
    {
        rd.AddForce(Vector3.right*speedAutoMove);//前进
    }
    private void PlayerMoveUpandDown()
    {
        float v = Input.GetAxis("Vertical");//上下运动
        rd.AddForce(v*Vector3.up*speedMoveUpandDown);//给一个上下的力量
    }
}

当我们小球向前移动时,我们需要镜头跟着小球一起移动,在camera上添加如下代码:

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

public class cameraControl : MonoBehaviour {
    public GameObject player;
    private float offset_camera;
	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);
    }
}

现在开始设置障碍物,新建一个cube挨在墙体上,我们要利用代码来自动生成前方的障碍物和自动删除后方的障碍物。
新建一个empty取名为barrierControl,在barrierControl里新建script代码,代码如下:

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

public class BarrierControl : MonoBehaviour {
    public GameObject player;
    public GameObject BarrierPre;
    public GameObject CurrentBarrier;
    public int BarrierInterval = 10;
	
	void Start () {
		
	}
	
	void Update () {
        AutCreatBarrier();

    }
    public void AutCreatBarrier()
    {
        if(player.transform.position.x>CurrentBarrier.transform.position.x)
        {
            //生成新的障碍物
            float targetX = CurrentBarrier.transform.position.x+BarrierInterval;
            float targetY = RandomBarrierPositionY();
            Vector3 targetPos = new Vector3(targetX,targetY,0);
            GameObject g = Instantiate(BarrierPre,targetPos,Quaternion.identity);
            g.transform.localScale = new Vector3(g.transform.localScale.x, RandomBarrierSizeY((int)g.transform.position.y),g.transform.localScale.z);
            CurrentBarrier = g;
        }
    }
    public float RandomBarrierSizeY(int y)
    {
        int yAbs = Mathf.Abs(y);
        if(yAbs==0)
        {
            return 6;
        }
        else
        {
            return (3 - yAbs) * 2 - 1;
        }

    }
    public float RandomBarrierPositionY()
    {
        int r = Random.Range(-3,3);
        return r;
    }
}



写完这些代码后在inspector里插入player和barrier,在这时候已经会有新的障碍物生成了,此时虽然有障碍物生成,但是生成的障碍物位置都是固定的,此时我们需要构造一个函数,AutoDestoryBarrier的代码:

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

public class AutoDestoryBarrier : MonoBehaviour {

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

wallControl部分代码:

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

public class wallControl : MonoBehaviour {
    private float offset;
    public GameObject player;
	// Use this for initialization
	void Start () {
        offset = gameObject.transform.position.x - player.transform.position.x;
	}
	
	// Update is called once per frame
	void Update () {
        FollowPlayerMove();
	}
    void FollowPlayerMove()
    {
        gameObject.transform.position = new Vector3(player.transform.position.x+offset,0,0);
    }
}



总结

这个游戏比较简单,也有很多的不足之处,欢迎大家指导修改!感谢观看!


http://www.niftyadmin.cn/n/4556147.html

相关文章

Java8自定义函数式编程接口和便捷的引用类的构造器及方法

什么是函数编程接口&#xff1f; 约束&#xff1a;抽象方法有且只有一个&#xff0c;即不能有多个抽象方法&#xff0c;在接口中覆写Object类中的public方法&#xff08;如equal&#xff09;&#xff0c;不算是函数式接口的方法。 被FunctionalInterface注解该接口&#xff0c;…

02linux 基本命令

系统环境变量的设置 以nginx为例&#xff1a; 方式一&#xff1a; ~/.bash_profile 这是个人的 /etc/profile 为系统的环境变量 vim /etc/profile.d/nginx.sh#在该文件中配置&#xff1a; export PATH /opt/nginx-1-12/sbin:$PATH# 然后执行 . /etc/profile.d/nginx.sh# 然后…

解决 HomeBrew 下载缓慢的问题

macOS 自身不提供包管理器&#xff0c;常用的包管理器有 HomeBrewMacPortsMacPorts 第一次使用要 build 整个基本库&#xff0c;编译时间很长。优点是不怎么依赖系统&#xff0c;更新 macOS 不会破坏现有的包&#xff0c;以及可以使用所有的开源包。 HomeBrew 尽量使用系统现有…

谁是面向对象设计中的霸主?(上)

版权声明&#xff1a;本文为博主原创文章。未经博主同意不得转载。 https://blog.csdn.net/u010929604/article/details/26051679 在面向对象的世界里&#xff0c;设计模式从来就没有最好最强的&#xff0c;各个模式从来就谁也不服谁。彼此明争暗斗&#xff0c;未曾善罢甘休。今…

Linux虚拟系统安装——Ubuntu18.04 CentOS6.5

Linux虚拟系统安装——Ubuntu18.04 & CentOS6.5 摘要&#xff1a;Linux简介、虚拟系统安装、系统备份与文件介绍 1. Linux简介 &#xff08;1&#xff09;1968年&#xff0c;MIT、Bell实验室、美国通用电气公司共同开发Multic项目&#xff1b; &#xff08;2&#xff09;19…

贪吃蛇(C语言)

Snake.c #include<stdio.h> #include<stdlib.h> #include <time.h> #include <windows.h> #include <conio.h>

VUE 动态修改Video标签Src播放路径

首先HTML&#xff0c;注意这里修改 bind绑定src 属性是不能生效的 我使用的是用JS 获取DOM节点&#xff0c;然后给DOM的Src属性赋值 <video id"playVideos" controls width"100%" height"240"><source id"playVideosss" :src…

六.类的封装

类的封装 源程序&#xff1a; package lianxi;public class Account {String zhanghao;double cunkuanyu_e;Account (String hao,double a){zhanghaohao;cunkuanyu_ea; }//存款之后 并得到余额 //有返回值带参数的方法double cunkuan(Double cunqian) { if(cunqian<0)…