Unity|小游戏复刻|见缝插针1(C#)

准备
  1. 创建Scenes场景,Scripts脚本,Prefabs预制体文件夹
修改背景颜色
  1. 选中Main Camera

  2. 找到背景

  3. 选择颜色,一种白中透黄的颜色

创建小球
  1. 将文件夹里的Circle拖入层级里

  2. 选中Circle,位置为左右居中,偏上,颜色为黑色,大小缩为0.7

分数
  1. 创建Text

  2. 删除掉EventSystem,因为本例中UI不需要做任何事件

  3. 选中Text,点击重置,文本居中,颜色为白色,内容为0

  1. 选中Canvas,将渲染模式改为世界空间,接着修改大小,改为100.100
  1. 将缩放改为0.01,拖动到小球上

  2. 使得Circle和Canvas的位置坐标一致,都为0,2,0

  3. 选择MainCamera设置为Canvas的事件摄像机

小球运动
  1. 创建一个脚本,选择MonoBehaviour,命名为RotateSelf

  2. 将此脚本挂载到Circle的下面

  3. 编写脚本

c# 复制代码
using JetBrains.Annotations;
using UnityEngine;

public class NewMonoBehaviourScript : MonoBehaviour
{
    public float speed = 90;

    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        transform.Rotate(new Vector3(0, 0, speed * Time.deltaTime));
    }
}
  1. 在场景中点击播放,可以看到Circle在逆时针旋转,如果想要顺时针旋转,在speed前加一个负号
针头
  1. 将针导入到层级里,将Pin的大小适当放大
  1. 制作针的尾部,复制一个Circle

  2. 调整大小位置颜色

  1. 将Pin拖入Prefabs文件夹里,方便进行实例化创建

  2. 给针头添加碰撞器,选中针头,在检查器底下的添加组件,搜Circle Collider 2D添加

生成针
  1. 创建两个空对象,将第一个命名为StartPosition

  2. 将Pin放到StartPosition的下面,这样可以进行预览,调整位置

  1. 将StartPosition复制,拖到屏幕外面进行实例化
  1. 然后删去针,改名为SpawnPositon

  2. 创建一个空对象,命名为GameManager

  3. 创建一个GameManager脚本,挂载到对象下面

c# 复制代码
using UnityEngine;

public class GameManager : MonoBehaviour
{
    private Transform startPosition;
    private Transform spawnPosition;

    public GameObject pinPrefab;

    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        startPosition = GameObject.Find("StartPosition").transform;
        spawnPosition = GameObject.Find("SpawnPosition").transform;
        SpawnPin();
    }

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

    void SpawnPin()
    {
        GameObject.Instantiate(pinPrefab, spawnPosition.position, pinPrefab.transform.rotation);
    }
}
  1. 将PIn实例拖到右边的Pin Prefab里

  2. 点击运行后屏幕外生成了针

相关推荐
在路上看风景1 小时前
1.12 Memory Profiler Package - Summary
unity
SmalBox6 小时前
【URP】Unity Shader Tags
unity·渲染
极客柒14 小时前
Unity 塔防自用可视化路点寻路编辑器
unity·编辑器·游戏引擎
程序猿多布14 小时前
Unity AssetBundle详解
unity·assetbundle
萘柰奈19 小时前
Unity学习----【进阶】Addressables(二)--加载资源与打包及更新
学习·unity
lvcoc1 天前
unity 接入火山引擎API,包括即梦AI
windows·unity·ai·火山引擎
王家视频教程图书馆1 天前
2025年最新 unityHub游戏引擎开发2d手机游戏和桌面游戏教程
游戏·unity·游戏引擎
SmalBox1 天前
【URP】法线贴图为什么主要是蓝色的?
unity·渲染
2301_793116943 天前
Unity 解决天空盒中间出现一条线
unity
佩京科技VR3 天前
禁毒教育展厅互动设备-禁毒教育基地-禁毒体验馆方案-VR禁毒教育软件
unity·vr·禁毒展厅·vr禁毒学习机