闲来学习一下 unity3d 的Demo,记录如下。

官方 Demo,名字为 Roll-A-Ball,如图

  

  场景比较简单,包含地面、玩家精灵、主摄像机、墙壁、可拾取的方块、分数为示 text、平行光源。

  资源目录下,包含材质、预制件、脚本。

  材质:定义了背景、小方块所需的材质。

  预制件:场景中共有12个小方块,所以先做一个 PickUp 的预制件。制作一个预制件,先向场景中创建一个3d对象cube,为该cube对象添加钢体组件、脚本组件,然后把该 cube 对象拖进一个空的预制作,这样便可方批量创建,现在向场景再拖入11个该预制件。

  脚本:摄像机脚本、玩家精录脚本、自转脚本。脚本是这个小游戏灵魂,控制各移动逻辑,下面会详述。

一、Rotator.cs 脚本绑定到小方块预制件:

using UnityEngine;
using System.Collections;

public class Rotator : MonoBehaviour {
    // Before rendering each frame..
    void Update () 
    {
        // Rotate the game object that this script is attached to by 15 in the X axis,
        // 30 in the Y axis and 45 in the Z axis, multiplied by deltaTime in order to make it per second
        // rather than per frame.
        transform.Rotate (new Vector3 (15, 30, 45) * Time.deltaTime);
    }
}    
核心就一句代码,绑定该脚本的GameObject每秒 绕指定轴旋转。

二、CameraController.cs脚本绑定到摄像机。

using UnityEngine;
using System.Collections;

public class CameraController : MonoBehaviour {

// store a public reference to the Player game object, so we can refer to it's Transform
    public GameObject player;

// Store a Vector3 offset from the player (a distance to place the camera from the player at all times)
    private Vector3 offset;

// At the start of the game..
    void Start ()
    {
        // Create an offset by subtracting the Camera's position from the player's position
        offset = transform.position - player.transform.position;
    }

// After the standard 'Update()' loop runs, and just before each frame is rendered..
    void LateUpdate ()
    {
        // Set the position of the Camera (the game object this script is attached to)
        // to the player's position, plus the offset amount
        transform.position = player.transform.position + offset;
    }
}

在Start方法中,记下摄像机与玩家精灵的距离差值,在LateUpdate方法中更新摄像机的位置,使摄像机跟随玩家精录的移动。

三、PlayerController.cs绑定到 player 对象。

using UnityEngine;

// Include the namespace required to use Unity UI
using UnityEngine.UI;

using System.Collections;

public class PlayerController : MonoBehaviour {
    
    // Create public variables for player speed, and for the Text UI game objects
    public float speed;  //精灵移动速度
    public Text countText;//分数 Text
    public Text winText;//胜利 Text

// Create private references to the rigidbody component on the player, and the count of pick up objects picked up so far
    private Rigidbody rb;//精灵本身绑定的钢体对像
    private int count;//分数记数

// At the start of the game..
    void Start ()
    {
        // Assign the Rigidbody component to our private rb variable
        rb = GetComponent<Rigidbody>();//钢体对象附值

// Set the count to zero 
        count = 0;//初始化分数

// Run the SetCountText function to update the UI (see below)
        SetCountText ();//设置初始化文本

// Set the text property of our Win Text UI to an empty string, making the 'You Win' (game over message) blank
        winText.text = "";
    }

// Each physics step..  处理输入
    void FixedUpdate ()
    {
        // Set some local float variables equal to the value of our Horizontal and Vertical Inputs

  //取得键盘输入的 x 方向、y方向
        float moveHorizontal = Input.GetAxis ("Horizontal");
        float moveVertical = Input.GetAxis ("Vertical");

  //x 方向、y 方向组成方向向量
        // Create a Vector3 variable, and assign X and Z to feature our horizontal and vertical float variables above
        Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);

// Add a physical force to our Player rigidbody using our 'movement' Vector3 above, 
        // multiplying it by 'speed' - our public player speed that appears in the inspector

  //给精灵的钢体对象一个该方向向量上的、大小为 speed 的力
        rb.AddForce (movement * speed);
    }

// When this game object intersects a collider with 'is trigger' checked, 
    // store a reference to that collider in a variable named 'other'..

 //处理碰撞逻辑
    void OnTriggerEnter(Collider other) 
    {
        // ..and if the game object we intersect has the tag 'Pick Up' assigned to it..

  //与精灵碰撞的对象,如果 tag 是 Pick Up,那么隐藏,同时记数+1,更新文本显示
        if (other.gameObject.CompareTag ("Pick Up"))
        {
            // Make the other game object (the pick up) inactive, to make it disappear
            other.gameObject.SetActive (false);

// Add one to the score variable 'count'
            count = count + 1;

// Run the 'SetCountText()' function (see below)
            SetCountText ();
        }
    }

// Create a standalone function that can update the 'countText' UI and check if the required amount to win has been achieved

 // 文本更新方法,胜利判定逻辑
    void SetCountText()
    {
        // Update the text field of our 'countText' variable
        countText.text = "Count: " + count.ToString ();

// Check if our 'count' is equal to or exceeded 12
        if (count >= 12) 
        {
            // Set the text value of our 'winText'
            winText.text = "You Win!";
        }
    }
}

unity3d入门 Demo 学习记录的更多相关文章

  1. Python入门基础学习记录(二)汇率案例学习记录

    一.汇总整理 1.操作 ①新建python文件 工程右键--new--python file 2.注意问题与知识点 >变量定义:直接写变量名即可,例如定义一个字符串并赋值123: rmb_str ...

  2. [Unity3D]做个小Demo学习Input.touches

    [Unity3D]做个小Demo学习Input.touches 学不如做,下面用一个简单的Demo展示的Input.touches各项字段,有图有真相. 本项目已发布到Github,地址在(https ...

  3. Vue学习记录第一篇——Vue入门基础

    前面的话 Vue中文文档写得很好,界面清爽,内容翔实.但文档毕竟不是教程,文档一上来出现了大量的新概念,对于新手而言,并不友好.个人还是比较喜欢类似于<JS高级程序设计>的风格,从浅入深, ...

  4. 《java从入门到精通》学习记录

    目录 <Java从入门到精通>学习记录 3 基础的基础部分: 3 一. 常量与变量 3 1. 掌握: 3 (1) .常量与变量的声明方式: 3 (2) .变量的命名规则: 3 (3) .变 ...

  5. Git学习记录 力做全网最强入门教程

    目录 Git学习记录 力做全网最强入门教程 什么是GitHub? 什么是Git? Git的配置 Git的安装(只介绍windos操作系统下) Git的配置 至此我们的入门教程到此结束,更新中级教程要等 ...

  6. Unity3D Adam Demo的学习与研究

      1.简述 这篇文章是对Adam各种相关资料了解后进行一些精简的内容.如果你想仔细研究某个技术请跳转至unity相关页面. Adam官方页面: https://unity3d.com/cn/page ...

  7. redis入门学习记录(二)

    继第一节 redis入门学习记录(一)之后,我们来学习redis的基本使用. 接下来我们看看/usr/local/redis/bin目录下的几个文件作用是什么? redis-benchmark:red ...

  8. Elasticsearch学习记录(入门篇)

    Elasticsearch学习记录(入门篇) 1. Elasticsearch的请求与结果 请求结构 curl -X<VERB> '<PROTOCOL>://<HOST& ...

  9. 我的three.js学习记录(二)

    通过上一篇文章我的three.js学习记录(一)基本上是入门了three.js,但是这不够3D,这次我希望能把之前做的demo弄出来,然后通过例子来分析操作步骤. 1. 示例 上图是之前做的一个dem ...

随机推荐

  1. NX二次开发-UFUN单对象选择对话框UF_UI_select_with_single_dialog

    #include <uf.h> #include <uf_ui.h> ], void* user_data, UF_UI_selection_p_t select) { if ...

  2. NX二次开发-NXString转换为char*方法

    NX9+VS2012 #include <uf.h> #include <uf_drf.h> #include <NXOpen/Annotations_Note.hxx& ...

  3. 3.RabbitMQ 第一个程序

    RabbitMQ消息服务器主要解决应用程序之间异步消息传输问题,传统的MQ分为点对点和主题与订阅,RabbitMQ使用Exchange(交换机)实现更加灵活的消息传递. 前面介绍过几个概念,Routi ...

  4. 探索NDIS HOOK新的实现方法(1)

    NDIS HOOK是专业级防火墙使用的一种拦截技术,NDIS HOOK的重点是如何获得特定协议对应NDIS_PROTOCOL_BLOCK指针,获得了该指针,接下来就可以替换该协议所注册的收发函数,而达 ...

  5. RoadFlow开源工作流源码-项目架构分析

    项目文件结构: 很明了一个标准的三层架构的系统. 表示层:Web 业务层:Business 数据访问层:Data 另外存在缓存层:Cache缓存 增加公共使用类库:Utility 下面以一个实例(系统 ...

  6. Docker Api 实测

    好久没写博客,工作中想着未来部门需要对docker进行维护相对麻烦,而且,网络上也缺少一些合适的项目,于是准备筹划自己动手.先找到了Docker 的API文档,地址是:https://docs.doc ...

  7. 20140415 HOG 不同继承方式的访问特性 虚函数工作原理

    1.HOG block重叠的好处 由于行人通常其形状可以视为柔体,人 的边缘位置不固定,而有一些移动,block 重叠后,一个边缘的梯度信息在两个相邻重叠的 block 中都能有所表达,这样即使边缘的 ...

  8. 绿色版mysql 免安装使用(转载)

    MySQL绿色版的安装(mysql-5.6.22-win32.zip) Posted on 2015-01-31 23:21 卒子 阅读(10739) 评论(2) 编辑 收藏 由于工作需要最近要开始研 ...

  9. 数据分析相关概念(numpy)

    矢量 矢量是指一堆形成的集合. 多维数组也叫做矢量化计算. 单独一个数叫做标量 例: import datetime as dt import numpy as np n2=10000 start2 ...

  10. SonarQube代码质量扫描持续集成

    1.安装JDK和配置JAVA_HOME和CLASSPATH 2.安装mysql数据库 3.创建数据库和用户 mysql -u root -p mysql> CREATE DATABASE son ...