自学内容网 自学内容网

3.1刚体,力,碰撞体,触碰体

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test : MonoBehaviour
{
    Rigidbody rb,rig1,rig2;
    // Start is called before the first frame update
    void Start()
    {
        rb= transform.GetComponent<Rigidbody>();

        // rb.AddForce( new Vector3(0, 0, 1000f));
        rig1 = GameObject.Find("Target").GetComponent<Rigidbody>();      
        rig2 = GameObject.Find("Target1").GetComponent<Rigidbody>();      
    }

    // Update is called once per frame
    void Update()
    {
        //添加绝对力
        // rb.AddForce(new Vector3(0, 0, 10f));
        //添加相对力
        // rb.AddRelativeForce( new Vector3(0, 0, 10f));
        //添加扭曲力
        // rb.AddTorque(new Vector3(0, 0, 10f));
        //添加相对扭曲力
        //rb.AddRelativeTorque(new Vector3(0, 0, 10f));

        //if(Input.GetKeyDown("a"))
        //{
        //家家爆炸力
        //    //rb.AddExplosionForce(500f, new Vector3(0, 0, 10f), 10f);
        //    rb.AddExplosionForce(1500f, transform.position, 10f);
        //    rig1.AddExplosionForce(1500f, transform.position, 10f);
        //    rig2.AddExplosionForce(1500f, transform.position, 10f);
        //}

        if (Input.GetKeyDown("a"))
        {
            //刚体休眠
            rb.Sleep();
        }
        else if (Input.GetKeyDown("b"))
        {
            //刚体唤醒
            rb.WakeUp();
        }


    }

    private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.name=="dimian")
        {
            Debug.Log("发生了碰撞:" + collision.gameObject.name);
        }
       
    }

    private void OnCollisionExit(Collision collision)
    {
        Debug.Log("结束了碰撞");
    }

    private void OnCollisionStay(Collision collision)
    {
        Debug.Log("停留在了碰撞");
    }

    //======================

    private void OnTriggerEnter(Collider collider)
    {
        if (collider.gameObject.name == "dimian")
        {
            Debug.Log("发生了触发:" + collider.gameObject.name);
        }

    }

    private void OnTriggerExit(Collider collider)
    {
        Debug.Log("结束了触发");
    }

    private void OnTriggerStay(Collider collider)
    {
        Debug.Log("停留在了触发");
    }
}

原文地址:https://blog.csdn.net/qq_30003129/article/details/143076689

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!