c#Unity基础方法归纳(2)

携程的调用

void Start()
    {
        print("task1");
        print("task2");
        //print("task3");
        StartCoroutine("Task3");//开启携程方法
        print("task4");
    }
    void Update()
    {
        if (Input.GetKey(KeyCode.Space))
        {
            StopCoroutine("Task3");
        }
    }
    IEnumerator Task3()
    {
        yield return new WaitForSeconds(2f);//停顿2s
        print("task3");
    }

刚体的移动(虚拟轴移动)

还是不能忘了写完了给自己写的“形参” r 传对象

	private Rigidbody r;
    // Start is called before the first frame update
    void Start()
    {
        r = gameObject.GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void Update()
    {
        //刚体的移动
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        r.MovePosition(r.position+transform.right * h * Time.deltaTime * 2f);

        r.MovePosition(r.position + transform.forward * v * Time.deltaTime * 2f);
    }

碰撞事件

collision是碰撞参数的集合体,里面有你所需要的各种碰撞信息

	//碰撞事件检测
    private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.name!="Plane")
        {
            print("Enter: " + collision.gameObject.name);
        }
    }
    private void OnCollisionExit(Collision collision)
    {
        if (collision.gameObject.name!="Plane")
        {
            print("Exit:" + collision.gameObject.name);
        }
    }
    private void OnCollisionStay(Collision collision)
    {
        if (collision.gameObject.name!="Plane")
        {
            print("Stay: " + collision.gameObject.name);
        }
    }

触发器Trigger

碰撞之后就是触发器了,box collider 里面勾选is trigger,碰撞器变成了触发器。
碰撞触发三条件:

	//trigger事件,运动的物体必须带有collider和rigidbody
    //另一个物体必须至少有collider
    //其中一个勾选IsTrigger
    private void OnTriggerEnter(Collider other)
    {
        print("triggerEnter: " + other.gameObject.name);
    }
    private void OnTriggerExit(Collider other)
    {
        print("triggerExit: " + other.gameObject.name);
    }
    private void OnTriggerStay(Collider other)
    {
        print("triggerStay: " + other.gameObject.name);
    }
}

预制体的销毁(寿命

在创建预制体的时候经常会需要添加一个寿命,即自动销毁destroy()。
可以在prefabs(自创的预制体文件夹)里找的预制体,open prefab查看
也可以在对象的>直接切过去看

void Start()
    {
        Destroy(gameObject, 0.2f);//活不过2s
    }

物理射线的使用

同样的是触发机制。

// Start is called before the first frame update
private Ray ray;
private RaycastHit hit;
public GameObject obj;
// Update is called once per frame
//创建一条射线
//检测射线与其他物体的碰撞,得到碰撞信息
//通过碰撞信息对物体进行处理
void Update()
{
    //鼠标左键按下发射射线
    if (Input.GetMouseButtonDown(0))
    {
        //使用主摄像机创建一条射线,射线方向是到鼠标点击位置
        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        //使用物理类中的射线检测方法
    if (Physics.Raycast(ray,out hit))
    {
        Instantiate(obj, hit.collider.transform.position, hit.collider.transform.rotation);
        //将碰撞到的物体销毁
        GameObject.Destroy(hit.collider.gameObject);
    }
    }
}

以上是我菜(逃)学unity的代码笔记

发布了2 篇原创文章 · 获赞 0 · 访问量 16

猜你喜欢

转载自blog.csdn.net/qq_45946890/article/details/104673259