I'm having trouble figuring out how to get ray casting and the rocket working in the skeet shooter. On other projects I know how, but the fact that all the code is in one game manager script is confusing me. I'm not sure where and how to insert it in the script. My current game manager script is
Code:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class GameManager : MonoBehaviour { public Camera fpsCam; [Header("General Stuff")] public string activeWeapon; [Header("Target Launcher")] public GameObject launcherBase; public GameObject launcher; public int remainingPigeons = 10; public Rigidbody[] pigeons; //creates array of pigeons public float pigeonSpeed = 10f; [Header("Pistol")] public GameObject pistolPivot; [Header("Rocket")] public GameObject rocketPivot; public Rigidbody rocket; public float rocketSpeed = 20f; [Header("PSM")] public GameObject psmPivot; private void Start() { StartCoroutine(FirePigeons()); SwapWeapons(null); } private void Update() { if (Input.GetKeyDown(KeyCode.Alpha1)) { SwapWeapons(pistolPivot); activeWeapon = "Pistol"; } if (Input.GetKeyDown(KeyCode.Alpha2)) { SwapWeapons(rocketPivot); activeWeapon = "Rocket"; } if (Input.GetKeyDown(KeyCode.Alpha3)) { SwapWeapons(psmPivot); activeWeapon = "PSM"; } if (Input.GetButtonDown("Fire1")) { if(activeWeapon == "Pistol") { Shoot(); //Debug.Log("pew"); //raycast stuff } if (activeWeapon == "Rocket") { Debug.Log("boom"); //instantiate stuff start coroutine go to bottom make new ienumerator put instantiate stuff there yrn after code } if (activeWeapon == "PSM") { Debug.Log("whoosh"); } } } void Shoot() { RaycastHit thingIHit; if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.TransformDirection(Vector3.forward), out thingIHit)) { Debug.Log("pew"); } } void SwapWeapons(GameObject weaponToTurnOn) { iTween.RotateTo(pistolPivot, iTween.Hash( //makes movement for weapon rotation "z", 55f, "time", .5f, "islocal", true )); iTween.RotateTo(rocketPivot, iTween.Hash( "z", 55f, "time", .5f, "islocal", true )); iTween.RotateTo(psmPivot, iTween.Hash( "z", 55f, "time", .5f, "islocal", true )); if(weaponToTurnOn != null) //makes other weapon rotate down { iTween.RotateTo(weaponToTurnOn, iTween.Hash( "z", 0f, "time", 1f, "islocal", true, "easetype", "spring" )); } } IEnumerator FirePigeons() { while(remainingPigeons > 0) { float randomX; randomX = Random.Range(-40f, -8f); // random movement of launcher gun float randomY; randomY = Random.Range(-40f, 40f); //movement for base iTween.RotateTo(launcherBase, iTween.Hash( "y", randomY, "time", 2f, "easetype", "spring", "islocal", true )); iTween.RotateTo(launcher, iTween.Hash( "x", randomX, "time", 2f, "easetype", "spring", "islocal", true )); yield return new WaitForSeconds(3f); int randomBucketNumber; randomBucketNumber = Random.Range(0, pigeons.Length); //creates random shots from array Rigidbody clonedPigeon; clonedPigeon = Instantiate(pigeons[randomBucketNumber], launcher.transform.position, launcher.transform.rotation) as Rigidbody; clonedPigeon.velocity = launcher.transform.TransformDirection(Vector3.forward * pigeons[randomBucketNumber].GetComponent<TargetHealth>().speed); remainingPigeons = remainingPigeons - 1; } } }
Comment