課堂課程記錄——小球滾動
所有變量與物體名的命名原則都是見名知意
一、創(chuàng)建一個unity項目
二、Create所需3Dobject
1.Player2.walls
三、添加屬性:
1.添加在Player上
a.添加Rigidbody組件
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包含
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.實現(xiàn)相機跟隨
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變量賦值:
至此基本的小球滾動游戲就完成了。
繼續(xù)上節(jié)課的內容:
4.將player的形狀改為球形
左鍵選中player的屬性:
將mesh屬性由cube改為sphere
5.創(chuàng)建障礙預制體
a.先創(chuàng)建一個3D物體cube,將其命名為barrier。
b.在project的asset中創(chuàng)建prefab預制體文件
并將之前創(chuàng)建的barrier直接拖拽到prefab中。
若對prefab預制體的作用不理解的話,可訪問如下鏈接:
預制體的制作與功能
若barrier物體變?yōu)樗{色,則創(chuàng)建成功。
6.隨機生成障礙物
a.創(chuàng)建一個空物體,然后命名為barrierControl。
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;
}
}
到目前為止障礙物就能不斷的在與小球的距離控制下產(chǎn)生了。
7.障礙的清除
a.在之前的wall文件夾中創(chuàng)建一個新cube物體,命名為trigger,控制大小長度在略小于上下wall之間,以便過濾掉與其接觸的barrier(切記不要接觸上下的wall,否則游戲一開始就會將上下的wall給消除,小球一下就掉下去了)。
b.右鍵選中trigger,在右側的屬性欄,移除Cube(Mesh Filter)和Mesh Renderer屬性。
使trigger成為透明狀態(tài):
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.給障礙物添加隨機產(chǎn)生顏色功能:
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 () {
}
}
9.碰到障礙物顏色提示
C#代碼如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerColorControl : MonoBehaviour
{
private void OnCollisionEnter(Collision collision)
{
// Debug.Log("1");
//分數(shù)減少
UIcontrol._instance.AddScore(-10);
gameObject.GetComponent<Renderer>().material.color=Color.red;
}
private void OnCollisionExit(Collision collision)
{
gameObject.GetComponent<Renderer>().material.color=Color.white;
}
}
9.分數(shù)記錄
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中添加如下代碼進行分數(shù)增加
在Playcolorcontrol中添加分數(shù)減少代碼:
完整畫面如下:文章來源:http://www.zghlxwxcb.cn/news/detail-423062.html
以上小球酷跑課程內容就結束了,稍后我會對游戲進行進一步的完善功能。文章來源地址http://www.zghlxwxcb.cn/news/detail-423062.html
到了這里,關于unity——小球酷跑游戲制作的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!