【Unity3d学习笔记】刚体碰撞和关节

来源:互联网 发布:美工设计的岗位职责 编辑:程序博客网 时间:2024/04/26 14:34

3.20

RigidbodyConstant Forceusing UnityEngine;using System.Collections;public class AddForce : MonoBehaviour {void FixedUpdate (){if(Input.GetKeyDown(KeyCode.UpArrow)){  //当输入向上的按键时GetComponent<Rigidbody>().AddForce(0,60,0);  //给物体添加一个向上的力,这里用的是Unity5的写法,Unity4.x中一般用Rigidbody.AddForce()}}/*Rigidbody rb = GetComponent<Rigidbody> ();if(Input.GetKeyDown(KeyCode.UpArrow)){rb.AddForce(0,60,0);    //添加力的另一种写法}*/

3.21

using UnityEngine; using System.Collections; public class AddForce : MonoBehaviour { public float x_power = 100.0f; public float y_power = 0.0f; public float z_power = 0.0f; //给x,y,z轴添加变量的力void FixedUpdate (){          if(Input.GetKeyDown(KeyCode.RightArrow)){                    GetComponent().AddForce(x_power,y_power,z_power);                   }          } void OnCollisionEnter(Collision other){  //当物理碰撞进入时          Debug.Log (other.gameObject);  //输出被碰撞的物体的名字         } }

3.22

using UnityEngine;using System.Collections;public class CollisionTest : MonoBehaviour { void OnCollisionEnter(Collision other){  Debug.Log ("碰撞进入"); } void OnCollisionStay(Collision other){  Debug.Log ("碰撞持续"); } void OnCollisionExit(Collision other){  Debug.Log ("碰撞出去"); }}  //输出物体的不同状态

using UnityEngine;using System.Collections;public class CollisionTest : MonoBehaviour { void OnCollisionEnter(Collision other){  //当物体碰撞进入时  if (other.gameObject.name == "Cube") { //若被碰撞的物体是立方体   Destroy(other.gameObject); //则立方体消失  } }}

创建physic material,改变Bounciness,可以改变弹跳能力。


3.23

using UnityEngine;using System.Collections;public class TriggerTest : MonoBehaviour { void OnTriggerEnter(Collider other){//加在被碰撞的物体上  Debug.Log ("碰撞持续");  Rigidbody rb = other.GetComponent<Rigidbody>();  rb.AddForce(Vector3.up * 20,ForceMode.Acceleration); //Acceleration指加速度 }}


3.24

using UnityEngine;using System.Collections;public class AddForce : MonoBehaviour { void OnMouseDown(){  GetComponent<Rigidbody> ().AddForce (-Vector3.forward * 500); }}  //推拉门的一个效果,利用hinge joint





0 0
原创粉丝点击