Welcome!

Welcome to our community forums, full of great people, ideas and excitement. Please register if you would like to take part.

This is extra text with a test link..

Register Now

Announcement

Collapse
No announcement yet.

Help ray casting in skeet shooter

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Help ray casting in skeet shooter

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

  • #2
    You're on the right track there.

    In your Shoot function, where you have the Debug.Log("pew"), you can further fill that out to instantiate the bullet hole - exactly like we did for the Zombie shooter game.

    For the Rocket, you can make another function if you'd like (just like you did with Shoot). Maybe make this function called "ShootRocket" or something like that. Then call that ShootRocket function right under your Debug.Log("boom") line.

    Your ShootRocket method would have the code we used to instantiate a rocket and give it velocity. If you're feeling fancy, you can turn off the dummer RPG on the tip of the RPG when you instantiate the rocket with your effects, box collider and rigidbody.

    You're doing the right thing. Keep working with your strategy of setting up another method for each gun. That's not the only way to do it, but its a nice way to keep your large project organized.

    Comment


    • #3
      I got the gun and missiles working, but the missiles are not colliding with the animals. I've put a box collider and rigidbody on the missiles. Am I supposed to change the collision detection of the animals or missiles to something other than discrete?

      Comment


      • #4
        Is the box collider on the missile or on the parent object (the missile is pointed sideways, so to make it shoot straight, you likely needed to create an empty game Object).

        Comment


        • #5
          All my missiles are shooting straight since I created an empty game object. I currently have the box collider and rigidbody on the empty game object. Should I change it so that they're on the actual missile?

          Comment


          • #6
            Nope...attaching the box collider and rigid body to the parent is the right thing to do. Do you have a script on the parent gameObject so that when it hits something it explodes?

            Comment

            Working...
            X