【问题标题】:Bullet becomes bullet hole子弹变成弹孔
【发布时间】:2015-05-21 00:53:19
【问题描述】:

在我的项目中,我有枪。我的子弹使用刚体并在我开火时在枪管处产生。我的子弹穿过墙壁反弹,有时它穿过墙壁。

我知道,在物理射线投射中它非常简单:

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);
        }
    }
}

如何在刚体中做到这一点? Bullet 变成了粒子系统或弹孔图像。

我应该使用OnCollisionEnter() 还是OnTriggerEnter()

如何使它成为弹孔图像或粒子系统?

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    OnCollisionEnter 和 OnTriggerEnter 都可以使用;但是,请查看有关差异的解释:What is the difference between OnCollisionEnter and OnTriggerEnter?

    【讨论】:

      【解决方案2】:

      我在以下位置找到了这个 JavaScript:RAYCAST, BULLET HOLES, AND RANDOM ARRAYS

      你把它添加到你武器的发射器中:


      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
         }
      }
      

      【讨论】:

      • 我的名字说明了一切,你能把它翻译成c#吗? c;
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-24
      • 2012-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多