Unity3D C#脚本基础

来源:互联网 发布:辐射 赵船长 知乎 编辑:程序博客网 时间:2024/05/21 17:25
using UnityEngine;using System.Collections;public class reflect : MonoBehaviour {    // 引入一个游戏场景中的对象(包括抽象对象,如Transform.,最后要在图形界面拖入对象)    public GameObject muban;    // Use this for initialization    // 初始化调用    void Start () {        Debug.Log(muban.name);    }    //float ver = Input.GetAxis("Vertical");    //transform.position += transform.forward * ver * 300;    // Update is called once per frame    // Vector3 三维向量类    // 固定每一帧都会调用一次.    void Update () {        if (Input.GetKey(KeyCode.A)) // Input为一个类,GetKey() 为一个方法,如果用户输出为A ,则执行下列语句。            transform.position += new Vector3(-1, 0, 0) * Time.deltaTime;  //Time.deltaTime:  假如FPS为60,Time.deltaTime = 1/60;         // 与学过的东西好像不太一样,对象累加,x+(-1)*Time.deltaTime, y,z不变.     }    // 当进入触发器的范围时触发调用此函数 与之还有 OnTriggerExit,OnTriggerStay 顾名思义      void OnTriggerEnter(Collider other)  // other: 触发器所挂载的对象。    {        Debug.Log(other.name);        transform.position = new Vector3(0, 4, 0);    }}

触发器和碰撞机:

同属于Collider, 当不勾中 Is Trigger 时为碰撞机, 当触碰到碰撞机时会触发: OnCollisionEnter(Collision other)。都属于区域触发,不同点就是一个不能穿过会发生碰撞 ,一个能够穿过。

关于 Rigidbody 的 velocity : 意为 速度, 属于Vector3类, Vector3 为一个空间坐标向量类。

Vector3 v3 = new Vector3(1,0,0); 实例化 一个空间向量,它可以进行四则运算。
如: Rigidbody.velocity +=v3; 表示在x轴上的值加上1. v3 * 15 则 为(15,0,0);
它只是表示了一个速度向量,但是实际速度应该为 velocity.magnitude吧? , 15*15+15*15的平方?
在Unity3D 中,距离单位都用米来表示, 这里用秒来表示 那么速度就为 21米/秒?

速度向量和距离向量,正负都表示世界方向, 速度向量中的值代表速度,距离向量中的值代表速度?

如 transform.position += new Vector(1,0,0); 即代表 将游戏对象当前x上的位置加上一个单位;

using UnityEngine;using System.Collections;public class ballScript : MonoBehaviour {    Rigidbody rb;    // Use this for initialization    void Start () {        // 一开始给ball某个方向上的速度        rb = GetComponent<Rigidbody>();        rb.velocity = new Vector3(0, 1, -1) * 15 ;    }    // Update is called once per frame    void Update () {        Vector3 v = rb.velocity;        if (v.z == 0)            v.z = 1;        rb.velocity = v;    }    void OnTriggerEnter(Collider other) {        Vector3 v = rb.velocity;        if (other.name == "player") {            if(v.z <= 0) {                rb.velocity = new Vector3(0, 1, -1) * 15;                Debug.Log(rb.velocity.x);                Debug.Log(rb.velocity.y);                Debug.Log(rb.velocity.z);                Debug.Log(rb.velocity.magnitude);            }            else            {                rb.velocity = new Vector3(0, 1, 1) * 15;            }        }    }    void OnCollisionEnter(Collision other) {        if (other.collider.tag == "Finish") {            Destroy(other.gameObject);        }    }}

第一天的学习心得:
一个游戏对象拥有许多的组件,我将其理解为一个特性, 比如新创建一个cube对象,开发者为其初始化了几个特性分别是:Mesh Filter(网格过滤器:即改变形状,正方形,圆形等,在引擎中形状都用网格表示),Collider (这个东西叫做碰撞机,如果没有它,则这个物体不能触碰到任何物体,那么它就可以引申出很多的属性,比如材质:木板?弹床?冰块等,材质又可以引申出许多属性 比如:摩擦力?弹性?) 与现实世界不同的就是碰撞机的大小可以改变,大于或者小于物体本身。,Mesh Renderer(网格渲染器:具体属性还不是很清楚待查看)。
以上为初始化属性。
当我们运行一个物体时,会发现它不会因为重力而往下坠落。 这是因为它没有这个属性。就像一个人它本身是有重力的,因为它有质量。但是开发者并没有为其初始化该特性(组件),这能够让我们够好得去开发。
为其添加一个刚体(Rigidbody: 它有 mass(质量),drag(我猜应该是物体的拉力)等)再运行这个物体就会掉落。我们想要发生碰撞的话就需要碰撞的两个物体都要 Collider 而且 一个物体要有刚体这个特性。

一个物体的具体行为则需要在脚本中实现然后将脚本拖给这个物体。 有趣的事情发生了, 当我们在脚本中编写一条属性时(public Rigidbody rb;),在Unity3D的图形界面的inspector中会出现这个属性,然后我们可以将场景中所有?相应类型的对象拖入到属性中,然后在脚本中就可以使用它了。

using UnityEngine;using System.Collections;public class player : MonoBehaviour {    // -11.27    //     // Use this for initialization    // 砖块预设体    public GameObject bricks;    void Start () {        // 循环创建brick        //Instantiate(bricks, new Vector3(-0.5f, 0, 0),Quaternion.identity);        float width = 1;        float widthSpace = 0.5f;        for (int i = 0; i < 13; i++) {            for(int j = 0;j<4;j++)                Instantiate(bricks, new Vector3(-0.5f, 6-j*2, 11.27f - i*2  ),Quaternion.identity);        }    }    // Update is called once per frame    void Update () {                // 获取水平轴上的值 [-1,1]                float hor = Input.GetAxis("Horizontal");                // 移动player                transform.position += new Vector3(0, 0, -1) * hor * 20 * Time.deltaTime;        if (transform.position.z >= 11.27)            transform.position = new Vector3(transform.position.x, transform.position.y, 11.27F);        else if (transform.position.z <= -11.27)            transform.position = new Vector3(transform.position.x, transform.position.y, -11.27F);    }}

打砖块脚本………………………

0 0
原创粉丝点击