栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > 其他

小球酷跑unity制作

其他 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

小球酷跑unity制作

小球酷跑unity

**1.环境搭建和移动就是两个长方体中间夹着一个小球然后让小球实现上下移动以及自动向右边行驶的代码如下
**

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

public class PlayerControl : MonoBehaviour
{
    public float speedAutoMove = 5;
    public float speedMoveUpAndDown = 5;
    private void Update()
    {
        PlayerAutoMove();
        PlayerMoveUpAndDown();
        PlayerMoveMaxSpeed();
    }
    public void PlayerAutoMove()
    {
        gameObject.GetComponent().AddForce(Vector3.right*speedAutoMove);
    }
    public void PlayerMoveUpAndDown()
    {
        float v = Input.GetAxis("Vertical");
        gameObject.GetComponent().AddForce(Vector3.up * speedMoveUpAndDown*v);
    }
    public void PlayerMoveMaxSpeed()
    {
        
            gameObject.GetComponent().velocity = new Vector3(5, gameObject.GetComponent().velocity. y, 0);
        
    }
    
   
}

接下来实现相机跟随小球运动

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

public class CameraControl : MonoBehaviour
{
    // Start is called before the first frame update
    private float offset;
    public GameObject player;

    void Start()
    {
        offset = gameObject.transform.position.x - player.transform.position.x;

    }

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

    }
}

以及墙体跟随小球运动

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

public class WallControl : MonoBehaviour
{

    private float offset;
    public GameObject player;
    private void Start()
    {
        offset = gameObject.transform.position.x - player.transform.position.x;
    }

    private void Update()
    {
        FllowPlayerMove();
    }

    public void FllowPlayerMove()
    {
        gameObject.transform.position = new Vector3(player.transform.position.x+offset, gameObject.transform.position.y, gameObject.transform.position.z);
    }
}

2.障碍自动生成前期准备
首先建立一个障碍物barrier障碍物根据坐标进行障碍的自动生成同时将障碍作为预制体`

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

public class BarrierControl : MonoBehaviour
{
    // Start is called before the first frame update
    public float barrierInterval = 5;
    public GameObject player;
    public GameObject CurrentBarrier;
    public GameObject barrierPerfab;
   

    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        AutoCreateBarriers();
    }
    public void AutoCreateBarriers()
    {
        if (player.transform.position.x > CurrentBarrier.transform.position.x)
        {
            UIContol._instance.AddScore(5);
            float targetX= CurrentBarrier.transform.position.x+ barrierInterval;
            float targetY= RandomBarrierPosition();
            Vector3 targetPos = new Vector3(targetX,targetY,0);
           GameObject g= Instantiate(barrierPerfab,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 Random.Range(0,8);
        }

        else
        {
            return Random.Range(1, ((4 - rAbs) * 2) + 1);
        }
    }
    public float RandomBarrierPosition()
    {
        int r = Random.Range(-4, 5);
        return r;
    }
}

3.自动销毁障碍
设置一个透明的长方体使其可以进行触发勾选is trigger选项使他触发时销毁方块代码如下

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

public class AutoDestroyBarriers : MonoBehaviour
{
    private void OnTriggerEnter(Collider other)
    {
        Destroy(other.gameObject);
    }
}

接下来实现障碍的随机颜色

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

public class Barrier : MonoBehaviour
{
    // Start is called before the first frame update
    public Material[] barrierMaterials;
    void Start()
    {
        int i = Random.Range(0, barrierMaterials.Length);
        gameObject.GetComponent().material = barrierMaterials[i];
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

4.碰到障碍提示
进行加减分以及颜色变换

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

public class playerCollision : MonoBehaviour
{
    private void OnCollisionEnter(Collision collision)
    {
        UIContol. _instance.AddScore(-3);
        gameObject.GetComponent().material.color = Color.red;
        
    }
    private void OnCollisionExit(Collision collision)
    {
        gameObject.GetComponent().material.color = Color.white;
    }
}

新建canvas建立计分板

using UnityEngine.UI;

public class UIContol : MonoBehaviour
{
    public Text scoreText;
    public int score=0;
    public static UIContol _instance;
    private void Awake()
    {
        _instance = this;
    }
    public void AddScore(int x)
    {
        score += x;
        scoreText.text = "得分:" + score;
    }
    
    }

同时优化了小球i速度

  public void PlayerMoveMaxSpeed()
    {
        
            gameObject.GetComponent().velocity = new Vector3(5, gameObject.GetComponent().velocity. y, 0);
        
    }

5。设置开始界面

进行按钮设置

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

public class GameManager : MonoBehaviour
{
    // Start is called before the first frame update
 public void OnStartGame(string sceneName)
    {
        SceneManager.LoadScene(sceneName);
    }
        
    }


使其点击开始游戏跳转场景
最后封装两个场景
大功告成

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/901618.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号