Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by dragonking300 · Dec 11, 2016 at 10:02 PM · c#scripting problemscripting beginner

My cannon that I am trying to make is spawning bricks from every direction and at 1 bullet per frame

My cannon is a unity square stretched to be a rectangular prism and when I loaded my scene my cannon starts spawning bricks from every direction. To fix this I need to know 2 things. How to use coroutines to make my bullet prefab spawn every few seconds. How to make bullets shoot from the same consistent direction

  using UnityEngine;
  using System.Collections;
  
  public class CannonScript : MonoBehaviour {
  
   
     public GameObject Cannon;
      public GameObject Bullet;
      public float Bullet_Foward_Force;
      void Start ()
      {
        
       
      }
  
   int counter = 0;
  
      void Update ()
      {
          counter++;
            if (counter < 50000)
          {
  
              GameObject temporary_Bullet_handler = Instantiate(Bullet, Cannon.transform.position, Cannon.transform.rotation) as GameObject;
              Rigidbody Temporary_Rigidbody;
              Temporary_Rigidbody = temporary_Bullet_handler.GetComponent<Rigidbody>();
              Temporary_Rigidbody.AddForce(transform.forward * Bullet_Foward_Force);
              counter = 0;
             
          } 
      }
  }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Konomira · Dec 11, 2016 at 11:40 PM

This uses unity's time counters to detect time differences between frames

 float previousTime, waitTime;
 void Start()
 {
     previousTime = Time.timeSinceLevelLoad;
     waitTime = 5; // 5 seconds
 }
 void Update()
 {
     if (Time.timeSinceLevelLoad - previousTime > waitTime)
     {
         Fire();
         previousTime = Time.timeSinceLevelLoad;
     }
 }
 
 void Fire()
 {
     GameObject temporary_Bullet_handler = Instantiate(Bullet, Cannon.transform.position, Cannon.transform.rotation) as GameObject;
     Rigidbody Temporary_Rigidbody;
     Temporary_Rigidbody = temporary_Bullet_handler.GetComponent<Rigidbody>();
     Temporary_Rigidbody.AddForce(transform.forward * Bullet_Foward_Force);
 }

The method you used would work (I think), but the main problem I see is if (counter < 50000). It should be a >

Comment
Add comment · Share
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
avatar image
0

Answer by dragonking300 · Dec 12, 2016 at 12:22 AM

thx, it fires as it should but it doesn't fire is the direction I want it to no matter how I scale it. How do i fix that?

Comment
Add comment · Show 8 · Share
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
avatar image Konomira · Dec 12, 2016 at 12:42 AM 0
Share

Does it fire in the direction that it's facing? If you could provide an image of the bullet firing I'd be able to help.

avatar image dragonking300 · Dec 12, 2016 at 05:21 AM 0
Share

It flys relative to a cube I have positioned. The way it calculates where to start placing the bullet is the magnitude of the 2 distances and what I imagined to happen was this

[ ] = cube for magnitude(cube has no collison) [ ___] = cannon O = bullet --- = pathing of the bullet

what I want to happen [ ] o --- [_]

what actually happens [ ] [___] o---- (bullet spawns from behind the cannon and goes up in a arc like this --- > ^

so basicly when I try to have my cannon shoot bullets it shoots the bullets in a arc up that goes up and then slides down to fly off the ledge

avatar image Konomira dragonking300 · Dec 12, 2016 at 02:08 PM 0
Share

Do something like

 float bulletSpeed = 5f;
 bullet.transform.position = Vector3.$$anonymous$$oveTowards(cannon.transform.up, cube.transform.position,
 bulletSpeed);

The cannon.transform.up should place it 1 unit above the cannon, you can multiply this value to fine tune the position of the bullet's spawn

avatar image dragonking300 Konomira · Dec 12, 2016 at 04:09 PM 0
Share

Ok, now my cannon is firing bullets at 1 frame per second again. But it is firing bullets 1 frame per second after 5 seconds.

 using UnityEngine;
 using System.Collections;
 
 public class CannonScript : $$anonymous$$onoBehaviour
 {
 
     float previousTime, waitTime;
     public Transform Cannonballpos;
     public GameObject Bullet;
     public float ForceAmmount = 1000;
     void Start()
     {
         previousTime = Time.timeSinceLevelLoad;
         waitTime = 5;
     }
 
     int counter = 0;
 
     void Update()
     {
 
         if (Time.timeSinceLevelLoad - previousTime > waitTime)
         {
 
             Fire();
             previousTime = Time.timeSinceLevelLoad;
         }
     }
     void Fire()
     {
         Rigidbody CannonRigidbody;
         CannonRigidbody = Instantiate(Bullet, Cannonballpos.position, Cannonballpos.rotation) as Rigidbody;
         CannonRigidbody.AddForce(Cannonballpos.forward * ForceAmmount);
 
         counter = 0;
 
 
     }
 }

Show more comments
avatar image Konomira · Dec 12, 2016 at 04:52 PM 1
Share

Here, I made a unity project and tested this out. Just drag the cube onto the target variable and the cannonball onto the projectile variable, the rest should work itself.

 using UnityEngine;
 
 public class CannonScript : $$anonymous$$onoBehaviour
 {
 
     float previousTime;
     public GameObject 
         projectile,     //  The cannonball
         target,         //  The cube that the cannon points at
         cannonBallPos;  //  The spawn position of the cannon ball
     public float
         waitTime = 1,   //  Time between shots
         forceAmount = 1000;
 
     void Start()
     {
         previousTime = Time.timeSinceLevelLoad;
 
         transform.localScale = new Vector3(1, 1, 2);
 
         cannonBallPos = new GameObject("cannonBallPos");    //  Creates a new game object
         cannonBallPos.transform.parent = transform; //  Sets the parent as the cannon
         cannonBallPos.transform.position = transform.position + new Vector3(0, 0, 0.5f); //  $$anonymous$$oves to the front of the cannon
     }
 
     void Update()
     {
         transform.LookAt(target.transform);
         if (Time.timeSinceLevelLoad - previousTime > waitTime)
         {
             previousTime = Time.timeSinceLevelLoad;
             Fire();
         }
     }
 
     void Fire()
     {
         GameObject cannonBall;
         cannonBall = (GameObject)Instantiate(projectile, cannonBallPos.transform.position, cannonBallPos.transform.rotation);
         cannonBall.GetComponent<Rigidbody>().AddForce(transform.forward * forceAmount);
     }
 }

avatar image dragonking300 Konomira · Dec 13, 2016 at 12:54 PM 0
Share

Sorry for such a late response but I have to saythank you so much for going out of your way and making the script. Imma use it as a learning tool and to make the cannons in my game

avatar image Konomira dragonking300 · Dec 13, 2016 at 12:57 PM 0
Share

It's no problem, if you have any other questions feel free to ask.

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.

Follow this Question

Answers Answers and Comments

262 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 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 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 would I make a ListChangedEvent? 1 Answer

Unity 3D: How to make the gameobject speed increase continuously on tapping quickly? 1 Answer

Spawning 3D Objects according to Data at JSON Matrix 0 Answers

How to move the object to where the object is already pointing to? 1 Answer

"Follow Target" script with a prefab (C#) Help me please! 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