本文主要是介绍【unity游戏】小球酷跑小游戏开发,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
前言
想必很多人小时候都喜欢玩天天酷跑,这个游戏也是陪伴了很多人的童年,今天我们用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 framevoid 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 initializationvoid Start () {offset = gameObject.transform.position.x - player.transform.position.x;}// Update is called once per framevoid Update () {FollowPlayerMove();}void FollowPlayerMove(){gameObject.transform.position = new Vector3(player.transform.position.x+offset,0,0);}
}
总结
这个游戏比较简单,也有很多的不足之处,欢迎大家指导修改!感谢观看!
这篇关于【unity游戏】小球酷跑小游戏开发的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!