在我的项目中,我有枪。当我开火时,我的子弹使用刚体并在枪管上产生。我的子弹穿过墙壁弹跳,有时穿过墙壁。
嗯,我知道,在物理光线投影中,它非常简单:
public GameObject par;
public int damage;
void Update()
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));
if (Input.GetMouseButtonDown(0))
{
if (Physics.Raycast(ray, out hit, 100))
{
GameObject particleClone = Instantiate(par, hit.point, Quaternion.LookRotation(hit.normal)) as GameObject;
Destroy(particleClone, 2);
hit.transform.SendMessage("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
}
}
}
我如何在刚体中做到这一点? 子弹成为粒子系统或子弹孔图像。
我应该使用OnCollisionEnter()
还是OnTriggerEnter()
?
如何使其成为一个bullethole图像或粒子系统?
答案 0 :(得分:2)
可以使用OnCollisionEnter和OnTriggerEnter;但是,请看一下差异的这个解释:What is the difference between OnCollisionEnter and OnTriggerEnter?
答案 1 :(得分:1)
我在RAYCAST, BULLET HOLES, AND RANDOM ARRAYS
中找到了这个JavaScript你拿下它并添加到武器的启动器:
var bulletTex : GameObject[]; // creates an array to use random textures of bullet holes
function Update ()
{
var fwd = transform.TransformDirection(Vector3.forward); //casts our raycast in the forward direction
var hit : RaycastHit;
Debug.DrawRay(transform.position, fwd * 10, Color.green); //drays our raycast and gives it a green color and a length of 10 meters
if(Input.GetButtonDown ("Fire1") && Physics.Raycast(transform.position, fwd, hit, 10))
{ //when we left click and our raycast hits something
Instantiate(bulletTex[Random.Range(0,3)], hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal)); //then we'll instantiate a random bullet hole texture from our array and apply it where we click and adjust
// the position and rotation of textures to match the object being hit
}
}