using UnityEngine; using System.Collections; public class Rocket : MonoBehaviour { // The fly speed (used by the weapon later)public float speed = 2000.0f; // explosion prefab (particles)public GameObject explosionPrefab; // find out when it hit somethingvoid OnCollisionEnter(Collision c) { // show an explosion // - transform.position because it should be // where the rocket is // - Quaternion.identity because it should // have the default rotation Instantiate(explosionPrefab, transform.position,
yaratishda biz Unity zarralarini allaqachon import qilganmiz va ular uchun biz uchun juda yaxshi bo'lgan Portlash effekti mavjud.
Shunday qilib, keling, standart aktivlar -> zarrachalar-> "Eski qism" lar ostidagi Loyiha maydonini ko'rib chiqamiz va Kichik portlash Prefab-ni Roketamiz skriptidagi Endi portlash vaqti keldi. Va yana birdamlik bizga yordam beradi. Biz loyihani Portlash Prefab uyasiga tortamiz, shunda quyidagicha ko'rinadi:Yana bir narsa: odatdagidek portlash yana va yana portlashi mumkin, shuning uchun loyiha maydonida Kichik portlash prefabsini tanlaymiz va "Bir zarba" variantini yoqamiz: Yana bitta narsa: odatdagidek portlash yana va yana portlashi mumkin. Loyiha hududidagi kichik portlash prefabsini tanlang va "Bir zarba" variantini yoqing:
Mayli, yana C # skriptini yaratamiz, bu safar uni Shut deb nomlaymiz. Ushbu skript har bir o'yinchi sichqonchani bosganida yangi Raketani tarqatishi kerak. Keyin uni oldinga uchish uchun Rigidbody's AddForce usulidan foydalaniladi: using UnityEngine; using System.Collections; public class Shoot : MonoBehaviour { // Rocket Prefabpublic GameObject rocketPrefab; // Update is called once per framevoid Update () { // left mouse clicked?if (Input.GetMouseButtonDown(0)) { // spawn rocket // - Instantiate means 'throw the prefab into the game world' // - (GameObject) cast is required because unity is stupid // - transform.position because we want to instantiate it exactly // where the weapon is // - transform.parent.rotation because the rocket should face the // same direction that the player faces (which is the weapon's // parent. // we can't just use the weapon's rotation because the weapon is // always rotated like 45° which would make the bullet fly really // weird GameObject g = (GameObject)Instantiate(rocketPrefab, transform.position, transform.parent.rotation); // make the rocket fly forward by simply calling the rigidbody's // AddForce method // (requires the rocket to have a rigidbody attached to it)float force = g.GetComponent().speed; g.rigidbody.AddForce(g.transform.forward * force);
using UnityEngine; using System.Collections; public class Shoot : MonoBehaviour { // Rocket Prefabpublic GameObject rocketPrefab; // Update is called once per framevoid Update () { // left mouse clicked?if (Input.GetMouseButtonDown(0)) { // spawn rocket // - Instantiate means 'throw the prefab into the game world' // - (GameObject) cast is required because unity is stupid // - transform.position because we want to instantiate it exactly // where the weapon is // - transform.parent.rotation because the rocket should face the // same direction that the player faces (which is the weapon's // parent. // we can't just use the weapon's rotation because the weapon is // always rotated like 45° which would make the bullet fly really // weird GameObject g = (GameObject)Instantiate(rocketPrefab, transform.position, transform.parent.rotation); // make the rocket fly forward by simply calling the rigidbody's // AddForce method // (requires the rocket to have a rigidbody attached to it)float force = g.GetComponent().speed; g.rigidbody.AddForce(g.transform.forward * force); } } }