暑期游戏案例---滚小球

发布时间 2023-07-23 21:56:33作者: 不用去猜mk

游戏名称:滚小球

游戏玩法:玩家按下WASD操作小球进行方向移动,小球滚动撞击到场景中的金币后即收集成功,场景中所有金币收集完成后通关。

游戏实现

①小球移动

脚本:

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

public class PlayerController : MonoBehaviour
{
    public float moveForce;
    Rigidbody rb;


    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    void Update()    //每帧都要检测输入,故写在Update()
    {
        float h = 0;
        float v = 0;
        h =-Input.GetAxis("Horizontal");//右→1,左→-1,不按→0
        v =-Input.GetAxis("Vertical");

        rb.AddForce(new Vector3(h, 0, v).normalized * moveForce);
    }
}

注:需要在小球的Inspector中加入Rigidbody组件,设置moveForce的大小

 

 

②硬币的旋转、碰撞消失

脚本:

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

public class Coin : MonoBehaviour
{
    public float rotateSpeed;   

    void Update()
    {
        transform.Rotate(transform.right * rotateSpeed * Time.deltaTime);
    }

    private void OnTriggerEnter(Collider other)
    {
        Destroy(gameObject);
        FindObjectOfType<GameManager>().AddScore();
    }
}

注:硬币的Box colider组件中要勾选Is Trigger,使它成为触发器,设置rotateSpeed的大小

 

 

③得分系统、游戏音效

脚本:

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

public class GameManager : MonoBehaviour
{
    public TextMeshProUGUI scoreText;
    public GameObject winText;

    int score;
    int scoreToWin;

    public Transform GoldCoin;
    AudioSource au;

    public AudioClip getCoinFX;
    public AudioClip winFX;

    void Start()
    {
        au = GetComponent<AudioSource>();
        winText.SetActive(false);
        scoreToWin = GoldCoin.childCount;
    }

    public void AddScore()
    {
        au.clip = getCoinFX;
        au.Play();
        score++;
        scoreText.text = "Count: " + score;

        if(score>=scoreToWin)
        {
            winText.SetActive(true);
            au.clip = winFX;
            au.Play();
        }
    }
}

注:设置好对应资源后,对应拖拽到组件中,如下图

 

④小地图、摄像头跟随

脚本(小地图):

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

public class Marker : MonoBehaviour
{
    Transform parent;
    Vector3 angle;

    void Start()
    {
        parent = transform.parent;
        angle = transform.eulerAngles;
    }


    void Update()
    {
        transform.eulerAngles = angle;
        transform.position = parent.position + Vector3.up;
    }
}

注:小地图需要一个marker标记,脚本中会将marker相对小球的角度固定住

脚本(摄像头跟随):

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

public class CameraFollow : MonoBehaviour
{
    public float smoothFollowSpeed;
    Vector3 offset;
    public Transform target;
    
    // Start is called before the first frame update
    void Start()
    {
        offset = target.position - transform.position;
    }

    private void LateUpdate()
    {
        Vector3 targetPos = target.position - offset;
        transform.position = Vector3.Lerp(transform.position, targetPos, smoothFollowSpeed);
    }
}

注:设置smoothFollowSpeed大小,选取对应Target

游戏截图:

 

 

 感谢b站up主OneCredit,帮助我成功入门这款小游戏,讲得非常好!

传送门:BV1RZ4y1n7Ro,BV1va4y1v7bE