Unity的Mass、AddForce和碰撞、触发器

Mass质量  尽量0.1~10
Drag阻力,float,任意方向都受阻力
Use Gravity是否使用重力
Is kinematic是否遵循动力学
Collision Detection碰撞检测模式

1.AngularVelocity(角速度):表示刚体的角速度向量,数据类型为Vector3,该变量的方向既为刚体旋转轴方向,旋转方向遵循左手定则。
2.Velocity(位移速度):表示物体的位移速度值。
3.CenterOfMass(重心):通过调低物体的重心,可以使物体不因为其他物体的碰撞或者作用力而倒下。
4.DetectCollisions(碰撞检测开关):表示物体是否能够与其他物体产生碰撞效应。
5.InertiaTensor(惯性张量):描述物体转动惯量,类型为Vector3
using UnityEngine;
using System.Collections;

public class ro : MonoBehaviour {
    public float rorateSpeed = 90f;
    public float moveSpeed = 3f;
    // Use this for initialization
    void Start () {
        //GetComponent<Rigidbody> ().angularVelocity = Vector3.up * rorateSpeed;
        //GetComponent<Rigidbody> ().velocity = Vector3.forward * moveSpeed;
        GetComponent<Rigidbody> ().centerOfMass = Vector3.down * 0.5f;
        GetComponent<Rigidbody> ().detectCollisions = false;  //false不检测碰撞
    }
    
    // Update is called once per frame
    void Update () {
    
    }
}


if (Input.GetKeyDown (KeyCode.Space)) {
            //ForceMode默认是Force,施加力时,受质量影响
            //GetComponent<Rigidbody>().AddForce(Vector3.up*300f,ForceMode.Force);

            //Acceleration忽略本身质量,按默认值1
            //GetComponent<Rigidbody> ().AddForce (Vector3.up * 100f,ForceMode.Acceleration);
            //VelocityChange 忽略质量
            GetComponent<Rigidbody> ().AddForce (Vector3.up* 300f, ForceMode.VelocityChange);
            //钢体某个位置施加力
            GetComponent<Rigidbody> ().AddForceAtPosition (Vector3.up* 300f,transform.position+new Vector3 (0.5f,-0.5f,0));
        }
        }


using UnityEngine;
using System.Collections;

public class CollisionSC : MonoBehaviour {
    public Collider red;
    public Collider blue;
    // Use this for initialization
    void Start () {
        //忽略两个碰撞器之间的碰撞,true,忽略,false,不忽略
        Physics.IgnoreCollision (red, blue, true);
    }
    
    // Update is called once per frame
    void Update () {
    
    }
}
忽略两层碰撞器之间的碰撞,layers +两个层blue、red,将blue球都加到layout的blue层,同理红球。Edit-project Setting-Physics,blue和red的对勾取消。


//开始发生碰撞,参数是碰撞到的碰撞体
    void OnCollisionEnter(Collision other){
        Debug.Log (other.gameObject.name);
    }
    //碰撞中
    void OnCollisionStay(Collision other){
    
    }
    //碰撞结束
    void OnCollisionExit(Collision other){

    }

Is Trigger触发器,只触发方法,如碰撞功能等需要自己实现。
//触发器,碰撞时触发Enter方法
    void OnTriggerEnter(Collider other){
        Debug.Log (other.gameObject.name);
    }
    //碰撞中
    void OnTriggerStay(Collider other){

    }
    //碰撞结束
    void OnTriggerExit(Collider other){

    }


 

猜你喜欢

转载自blog.csdn.net/Star_MengMeng/article/details/123029183