• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by GamingOink · Mar 30, 2017 at 05:53 PM · projectile

Script Projectile Project

Hello! I am trying to make a game where the objective is to dodge projectiles. My projectile shooting script was shooting more than one bullet at a time when all I wanted was for it to shoot one projectile at a time. The objective of the code is to have a random position to shoot from generated, shoot from that position, wait for the projectile to PASS the player, then shoot the next projectile from a new randomly generated position. I changed my code to this because it would not shoot bullets one at a time before. Also can someone check my raycasting? I dont know if it is working.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine; 
 public class LaserShooter : MonoBehaviour
 {
 public GameObject projectile;
 private GameObject bullet;
 private int modChoose;
 private float speed = 100;
 private bool access;
 Vector3 pos1 = new Vector3 (-4, 2, 12);
 Vector3 pos2 = new Vector3 (0, 2, 12);
 Vector3 pos3 = new Vector3 (4, 2, 12);
 LayerMask laserDetect = 1 << 10;
 Vector3 direction = new Vector3 (0, 0, -1);
 Vector3 rayOrigin = new Vector3();
 Quaternion spawnRotation = Quaternion.Euler(90, 0, 0);
 void Start()
 {
  modChoose = Random.Range(1, 4);
  access = true;
  StartCoroutine(spawn());
 }
 IEnumerator spawn() 
 {
  if (access == true) {
      if (modChoose == 1) {
          rayOrigin = pos1;
          bullet = Instantiate (projectile, pos1, spawnRotation) as GameObject;
          bullet.GetComponent<Rigidbody> ().AddForce (-transform.forward * speed);
          StartCoroutine (checkAccess()); 
      }
      if (modChoose == 2) {
          rayOrigin = pos2;
          bullet = Instantiate (projectile, pos2, spawnRotation) as GameObject;
          bullet.GetComponent<Rigidbody> ().AddForce (-transform.forward * speed);
          StartCoroutine (checkAccess()); 
      }
      if (modChoose == 3) {
          rayOrigin = pos3;
          bullet = Instantiate (projectile, pos3, spawnRotation) as GameObject;
          bullet.GetComponent<Rigidbody> ().AddForce (-transform.forward * speed);
          StartCoroutine (checkAccess());
      }
  }
 }
 IEnumerator checkAccess() 
 {
  if (Physics.Raycast (rayOrigin, direction, 30)) {
      access = false;
      yield return new WaitForEndOfFrame ();
  } 
  else {
      access = true;
      modChoose = Random.Range (1, 4);
      StartCoroutine (spawn ());
  }
  }
  }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

98 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I make a projectile shoot from the middle of the screen?? 0 Answers

How to make my snowball model to actually shoot like a projectile? 0 Answers

Move an object in X seconds with varying height 1 Answer

,I'm having trouble rebounding multiple projectiles with the same tag off a game object. 0 Answers

shooting a projectile 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges