调用其它组件中成员

  通过GameObject(游戏物体)。

  Base class for all entities in Unity scenes。  是Unity场景里面所有实体的基类。

  可以理解为两个类间的访问,定义一个超类用其中一个类实现。

  默认的gameObject为当前组件。transform为变换,有常用属性position(Vector3三维向量)。

  熟记transform下属性和方法作用。

transform.translate() 平移,给定vector3,给定坐标系'物体坐标系或者世界坐标系'。由于每秒执行60次,可以用Time.deltaTime(增量时间:以秒计算,完成最后一帧的时间)放慢。


让cube在n个点间转动:


using UnityEngine;
using System.Collections;

public class TransformTest : MonoBehaviour {

    // Use this for initialization
public Transform[] point ;//n个点,在unity中控制长度和gameobject
public Transform nextPoint ;//下一个要运动到的点
public int index ;//当前位置,用于求下一个要运动到的点
void Start () {
index = ;
nextPoint = point[] ;
} // Update is called once per frame
void Update () {
if(Vector3.Distance(transform.position, nextPoint.transform.position)>0.1f){//transform默认为当前物体,同gameObject
transform.Translate(Vector3.Normalize(nextPoint.position-transform.position)**Time.deltaTime, Space.World) ;//normalize,向量标准化
        //这里要用世界坐标系,用物体坐标系的话,在改变cube的Rotation后出错
}else{
index = (index+)%point.Length ;
nextPoint = point[index] ;
}
}
}
旋转,正角度为左手,负角度右手。transform.Rotate (new Vector3(0, -1, 0));

改变父物体的比例,子物体也会改变,比例不为1:1,移动父物体,子物体同样移动。(transform.parent,获取父物体的transform)

transform.position输出的是世界坐标系下的坐标位置,transform.localPosition为在父物体下物体坐标系的坐标位置。

定时重复调用可以使用InvokeRepeating函数实现, 启动0.5秒后每隔1秒执行一次 DoSomeThing 函数

  void Start() {
    InvokeRepeating("DoSomeThing", 0.5f, 1.0f);
  }

监控键盘:
  Input.GetKeyDown(KeyCode.W) KeyCode中包含键盘所有键位
  Input.GetKeyUp(KeyCode.W)
  Input.GetKey(KeyCode.W)  按下时一直执行
    对三种不同的动作监控
//        if(Input.GetKey(KeyCode.W)){
// transform.Translate(Vector3.forward) ;
// }
// if(Input.GetKey(KeyCode.S)){
// transform.Translate(-Vector3.forward) ;
// }
// if(Input.GetKey(KeyCode.A)){
// transform.Rotate(-Vector3.up);
// }
// if(Input.GetKey(KeyCode.D)){
// transform.Rotate(Vector3.up);
// }
等同 transform.Translate(new Vector3(, , Input.GetAxis("Vertical"))) ;
transform.Rotate(new Vector3(, Input.GetAxis("Horizontal"), )) ;//获取轴 edit/projectsetting/input

Time.timeScale 改变游戏运行速度,0为暂停游戏,暂停时update继续执行(FixedUpdate()下完全停止)。

     用Time.timescale加速或者减速时,在FixedUpdate()下使用。

GameObject.tag   标签
GameObject.layer 层
都可自定义

GameObject.FindGameObjectsWithTag  返回GameObject[]

GameObject.FindGameObjectWithTag  返回GameObject

若在脚本AScript里想要获取BScript里的变量,先获取BScript所在GameObject的实例

例:

    GameObject mainCarm = GameObject.FindGameObjectWithTag("MainCamera") ;

然后通过mainCarm找到BScript实例

    BScript bScript = (BScript)mainCarm.GetComponent("BScript") ;

这样就可以使用BScript里的所有公共变量和方法。

GameObject.SendMessage("方法名"); 若本身调用,搜寻同级所有GameObject,若其他object调用,搜寻该object中的此方法

 

gameObject, vector and transform的更多相关文章

  1. 【Unity3D游戏开发】GameObject.Find()、Transform.Find查找隐藏对象 (十)

    GameObjectFindTransformFind查找游戏对象 前置条件 相关API 1 GameObjectFind 2 TransformFind 3 其他查找 实际测试 即使隐藏root节点 ...

  2. GameObject.Find与Transform.Find的区别

    1.GameObject.Find 函数原型: public static GameObject Find(string name); 说明:1.GameObject只能查找到active的物体 2. ...

  3. Gameobject.Find和Transform.Find应用区别

    using UnityEngine;using System.Collections; public class test : MonoBehaviour{ private GameObject ro ...

  4. Unity GameObject.Find 和 transform.Find

    transform.Find(""); 找到子游戏对象,找自己找不到,能找到未激活的子游戏对象. 括号里可以是游戏对象的名字,也可以是层级. GameObject.Find(&qu ...

  5. Unity3D_06_根据Transform、GameObject和Tag获取子对象集合

    导引: 因为项目中难免要多次进行获取子对象或者子对象的集合,所以写一个单独的类,用来做这些操作.然后再实际的项目中,只需要使用 transform 或者 gameobject 调用这些方法就可以快速的 ...

  6. [CareerCup] 18.10 Word Transform 单词转换

    18.10 Given two words of equal length that are in a dictionary, write a method to transform one word ...

  7. Unity3D学习笔记——递归+非递归遍历GameObject的子物体

    在Unity3D中没有提供直接的方法获取某个GameObject的子GameObject,但是所有的GameObject都有transform对象,所以,一般是通过获取子GameObject的tran ...

  8. Unity中的各种寻找GameObject方法

    1.GameObject.Find():寻找Hierarchy面板中的activie 不为false的游戏对象: 路径如官方事例写法: public class ExampleClass : Mono ...

  9. unity中遍历Transform的子物体

    1.遍历Transform直接子transform private void Start() { var Equipment = building.transform.FindChild(" ...

随机推荐

  1. Android IOS WebRTC 音视频开发总结(六四)-- webrtc能走多远我不知道,但这个市场真实存在

    本文主要总结目前都有哪些使用场景用到webrtc,文章最早发表在我们的微信公众号上,详见这里, 欢迎关注微信公众号blackerteam,更多详见www.blackerteam.com webrtc只 ...

  2. leetcode1:在数组中找2个数的和正好等于一个给定值--哈希

    package java_net_test; import java.util.HashMap; import java.util.Iterator; import java.util.Map; pu ...

  3. Xen、Openvz、KVM有什么区别?

    VPS的全称为Virtual Private Server,叫做虚拟专用服务器(Godaddy称之为Virtual Dedicated Server,VDS).就是利用各种虚拟化手段把单台物理服务器虚 ...

  4. HTTP状态

    HTTP状态码 当浏览者访问一个网页时,浏览者的浏览器会向网页所在服务器发出请求.当浏览器接收并显示网页前,此网页所在的服务器会返回一个包含HTTP状态码的信息头(server header)用以响应 ...

  5. C#流总结(文件流、内存流、网络流、BufferedStream、StreamReader/StreamWriter、TextReader/TextWriter)

    一.文件流 FileStream类主要用于读写磁盘文件.常用于向磁盘存储数据或读取配置文件. 读取文件: //文件流:读取 FileStream fileStream = File.Open(@&qu ...

  6. C# MongoDB--时区问题(差了8小时)

    原因:MongoDB中存储的时间是标准时间UTC +0:00C#的驱动支持一个特性,将实体的时间属性上添加上这个特性并指时区就可以了.例如:[BsonDateTimeOptions(Kind = Da ...

  7. 网站瓶颈分析—MYSQL性能分析

    一.关于慢查询设置和分析 查找慢查询参数 mysql> show variables like 'long%'; +-----------------+----------+ | Variabl ...

  8. 关于js中立即执行的匿名函数写法

    /*最流行的写法*/ (function() { alert("run!") })(); /* !号可以有1~正无穷个,所以这一种就可以衍生无数种方式 */ !!!(functio ...

  9. STM32F0_新建软件工程详细过程

    前言 由于ST公司推出比STM32F1性价比更高的F0芯片,现在市面上F0芯片的占有率也非常高.F0芯片属于M0内核,主频48M(当然,可以超频的,但尽量不要超的太多),资源大小可根据项目需求来选型. ...

  10. PAT乙级真题1003. 我要通过!(20)(解题)

    “答案正确”是自动判题系统给出的最令人欢喜的回复.本题属于PAT的“答案正确”大派送 —— 只要读入的字符串满足下列条件,系统就输出“答案正确”,否则输出“答案错误”. 得到“答案正确”的条件是: 1 ...