Tempat latihan menembak. Pemotretan Raycast Unity 3D

Bahan ajar untuk sekolah pemrograman. Bagian 17

Tutorial sebelumnya dapat ditemukan di sini:
  1. Pesawat luar angkasa





  2. Domino





  3. Burung Flappy





  4. Ruang gravitasi





  5. Platformer





  6. Pohon (plugin SpeedTree)





  7. Memodelkan rumah di SketchUp





  8. Rumah di hutan





  9. Efek hujan. Partikel





  10. Bilyar





  11. Karakter cair





  12. Menempel dan bekerja dengan Sistem Acara





  13. Penyintesis 3D Unity





  14. Kapal berbantalan udara





  15. Ragdolls di Unity 3D





  16. Bagaimana vektor bekerja. Bola Basket Unity 3D





Dalam proyek ini, kami akan mempertimbangkan proses pengerjaan:





  • dengan raycast dan vektor;





  • dengan metode kelas khusus lainnya;





  • dengan AudioSource dan dengan Rigidbody melalui kode;





  • tiga komponen utama bidikan yang secara psikologis memengaruhi pemain (suara, cahaya dan kilau, animasi, dan jejak bidikan);





  • Instansiasi prefabs.





Tema utama dari proyek ini tepatnya adalah siaran sinar dan vektor. Yang terakhir, perlu mencurahkan cukup banyak waktu setiap hari, dan menjelaskan cara kerjanya dengan contoh-contoh sederhana. Tetapi jika Anda berhasil menyelesaikan proyek dengan siswa dengan cepat, maka dalam pelajaran ini akan tepat untuk mempertimbangkan sistem mecanim.





Perintah eksekusi

Buat proyek baru, impor aset terlampir .





Selain sumber daya standar, paket memiliki plugin pihak ketiga untuk stiker lukisan. Karyanya tidak tercakup dalam konteks pelajaran ini.





Proyek pelajaran dibagi menjadi 2 bagian - jarak tembak dan granat.





Tempat latihan menembak

- . , , , , .





, , .





DecalShooter, , , . .

, . , Y , CapsuleCollider MeshCollider Convex. , , point light, , AudioSource , Rigidbody Continius Dynamic isKinematik. AudioSource PlayOnAwake .





Target, .





, . .





using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Target : MonoBehaviour {
    public GameObject light;
    public Rigidbody rig;
    public AudioSource src;
 
    bool enabled = true;
 
    // Use this for initialization 
    void Start() {
        rig = GetComponent<Rigidbody>();
        src = GetComponent<AudioSource>(); 
    }
 
    // Update is called once per frame 
    void Update() {
 
    }
 
    public void shoot() {
        if (!enabled) {
           return;
        }
 
        rig.isKinematic = false; 
        light.SetActive(false); 
        src.Play();
 
        enabled = false;
    }
 
}

      
      



 ​shoot,​ , . - DecalShooter  ​shoot. :





   if (Input.GetKeyDown(KeyCode.Mouse0)) {
            time = 0.3f; 
            ShootSource.Play(); 
            anim.Play("fire"); 
            Muzzleflash.SetActive(true);
            //   
            RaycastHit hitInfo;
            Vector3 fwd = transform.TransformDirection(Vector3.forward); 
            
            if (Physics.Raycast(transform.position, fwd, out hitInfo, 100f)) {
                GameObject go = Instantiate(
                    DecalPrefab,
                    hitInfo.point, 
                    Quaternion.LookRotation(
                        Vector3.Slerp(-hitInfo.normal, fwd, normalization) 
                    )
                ) as GameObject;
                go.GetComponent<DecalUpdater>().UpdateDecalTo(
                    hitInfo.collider.gameObject, 
                    true
                );
                Vector3 explosionPos = hitInfo.point;
                Target trg = hitInfo.collider.GetComponent<Target>();
                
                if (trg) {
                    trg.shoot();
                }
                
                Rigidbody rb = hitInfo.collider.GetComponent<Rigidbody>();
                
                if (rb != null) {
                    rb.AddForceAtPosition(fwd * power, hitInfo.point, ForceMode.Impulse);
                    Debug.Log("rb!");
                } 
            }
            //   
        }
      
      



hitInfo , ,  ​shoot.​ , , . , . , , . Target :





light.SetActive(false);
      
      







light.GetComponent<Light>().color = Color.red;
      
      



, .





.





, - .





using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
 
public class Lenght :  MonoBehaviour {
    public Text Dalnost;
    float rasstoyanie = 0; //     
 
    // Use this for initialization 
    void Start() {
 
    }
 
    // Update is called once per frame 
    void Update() {
        RaycastHit hitInfo;
        
        if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hitInfo, 200)) {
            rasstoyanie = hitInfo.distance;
            Dalnost.text = rasstoyanie.ToString();
        }
    }
}
      
      



FPScontroller/FirstPersonCharacter. Canvas , .

, , .

.





- . , . , .





using System.Collections;
using System.Collections.Generic; 
using UnityEngine;
using UnityEngine.UI;
 
public class Length :  MonoBehaviour {
    public Text Dalnost;
    float rasstoyanie = 0; //      
    public GameObject sharik;
 
    // Use this for initialization
    void Start() {
 
    }
 
    // Update is called once per frame 
    void Update() {
        RaycastHit hitInfo; 
        
        if(Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), outhitInfo, 200)) {
            rasstoyanie = hitInfo.distance; 
            Dalnost.text = rasstoyanie.ToString ();
            if(Input.GetKeyDown(KeyCode.Mouse1)) {
                GameObject go = Instantiate(
                    sharik, 
                    transform.position + Vector3.Normalize(hitInfo.point - transform.position), 
                    transform.rotation
                );
                Rigidbody rig = go.GetComponent<Rigidbody>();
                rig.velocity = Vector3.Normalize(hitInfo.point - transform.position) * 10;
            } 
        }
    } 
}
      
      



- ? , . . .. , , . . Grenade.





using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Grenade :  MonoBehaviour {
    public Transform explosionPrefab;
 
    void OnCollisionEnter(Collision collision) {
        ContactPoint contact = collision.contacts[0];
        
        // Rotate the object so that the y-axis faces along the normal of the surface
        Quaternion rot = Quaternion.FromToRotation(Vector3.up, contact.normal);
        Vector3 pos = contact.point; 
        Instantiate(explosionPrefab, pos, rot);
        Destroy(gameObject);
    }
}
      
      



, Body Convex, RIgidbody . .





, , , .





. , AudioSource PlayOnAwake , Spital Blend 90 3 .





Untuk mengetahui semua efek dan penyebaran Rigidbody dengan benar, Anda perlu membuat skrip lain. Kami akan menyebutnya Ledakan:





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

public class Explosion :  MonoBehaviour {
    public float radius = 5.0f;
    public float power = 10.0f;
    public GameObject svet;

    void Start() {
        Destroy(svet, 0.1f);

        Vector3 explosionPos = transform.position;
        Collider[] colliders = Physics.OverlapSphere(explosionPos, radius);

        foreach(Collider hit in colliders) {
            Rigidbodyrb = hit.GetComponent<Rigidbody>();
            if (rb != null) {
                rb.AddExplosionForce(power, explosionPos, radius, 3.0f);
            }
        }
    }
}
      
      



Anda perlu melemparkannya ke efek ledakan dan membuat prefab.





Prefab ini digunakan dalam skrip granat untuk membuat ledakan.





Selesai!








All Articles