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. 点击运行后屏幕外生成了针

相关推荐
SendSi7 小时前
Unity辅助工具_头部与svn
unity·svn·游戏引擎
程序员正茂21 小时前
Unity安卓Android从StreamingAssets加载AssetBundle
android·unity·assetbundle·streamingassets
free-elcmacom1 天前
<3D建模>.max文件转换为.fbx文件
unity·3dmax·建模·文件转换
PassionY1 天前
Unity DOTS从入门到精通之 C# Job System
unity·ecs·dots·jobsystem·burst·baker·entities
red_redemption1 天前
Unity--Cubism Live2D模型使用
unity·live2d·cubism sdk
咩咩觉主2 天前
C# &Unity 唐老狮 No.6 模拟面试题
开发语言·unity·面试·c#·游戏引擎·唐老师
HELLOMILI2 天前
第四章:反射-Reflecting Your World《Unity Shaders and Effets Cookbook》
游戏·unity·游戏引擎·游戏程序·图形渲染·材质·着色器
末零2 天前
Unity 取色板
unity·游戏引擎
无敌最俊朗@2 天前
Unity大型游戏开发全流程指南
unity·游戏引擎
虾米神探2 天前
Unity InputField + ScrollRect实现微信聊天输入框功能
unity·游戏引擎