Schießbude. Unity 3D Raycast-Aufnahme

Unterrichtsmaterialien für die Programmierschule. Teil 17

Frühere Tutorials finden Sie hier:
  1. Raumschiff





  2. Domino





  3. Flattervogel





  4. Schwerkraftraum





  5. Plattform





  6. Bäume (Plugin SpeedTree)





  7. Modellieren eines Hauses in SketchUp





  8. Haus im Wald





  9. Regeneffekt. Partikel





  10. Billard





  11. Flüssiger Charakter





  12. Festhalten und Arbeiten mit dem Ereignissystem





  13. Unity 3D-Synthesizer





  14. Luftkissenfahrzeug





  15. Ragdolls auf Unity 3D





  16. Wie Vektoren funktionieren. Unity 3D Basketball





In diesem Projekt werden wir den Arbeitsprozess betrachten:





  • mit Raycasts und Vektoren;





  • mit Methoden anderer benutzerdefinierter Klassen;





  • mit AudioSource und mit Rigidbody durch Code;





  • drei Hauptkomponenten einer Aufnahme, die den Spieler psychisch beeinflussen (Ton, Licht und Glühen, Animation und die Spur der Aufnahme);





  • Instanziierung von Fertighäusern.





Die Hauptthemen des Projekts sind genau Raycasts und Vektoren. Letzteres ist notwendig, jeden Tag viel Zeit zu investieren und anhand einfacher Beispiele zu erklären, wie sie funktionieren. Wenn Sie es jedoch geschafft haben, ein Projekt mit Schülern schnell abzuschließen, sollten Sie in dieser Lektion das Mecanim-System berücksichtigen.





Ausführungsreihenfolge

Erstellen Sie ein neues Projekt und importieren Sie das angehängte Asset .





Zusätzlich zu den Standardressourcen enthält das Paket ein Plugin eines Drittanbieters zum Malen von Abziehbildern. Seine Arbeit wird im Rahmen dieser Lektion nicht behandelt.





Das Unterrichtsprojekt ist in zwei Teile unterteilt - Schießstand und Granaten.





Schießbude

- . , , , , .





, , .





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 .





Um alle Effekte und die Verbreitung des Rigidbody korrekt zu ermitteln, müssen Sie ein anderes Skript erstellen. Wir nennen es Explosion:





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



Sie müssen es auf den Explosionseffekt werfen und ein Fertighaus erstellen.





Dieses Fertighaus wird im Granatenskript verwendet, um eine Explosion zu erzeugen.





Getan!








All Articles