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 nerdares · Aug 16, 2015 at 03:57 PM · instantiateshootingshipboatcannon

Shooting Cannons from a boat

Ok so I have 2 prefabs for the Cannon that my ship will shoot. They are called:

  CannonLeft
  CannonRight

The left cannon has this script:

 using UnityEngine;
 using System.Collections;
 
 public class Move_Cannon_Left : MonoBehaviour
 {
 
     // Use this for initialization
 
     public float speed = 1f;
     void Start () 
     {
     
     }
     
     // Update is called once per frame
     void Update () 
     {
         transform.Translate(speed, 0, 0);
     }
 }


The right one has this one:

 using UnityEngine;
 using System.Collections;
 
 public class Move_Cannon_Right : MonoBehaviour {
 
     // Use this for initialization
 
     public float speed = 1f;
     void Start()
     {
 
     }
 
     // Update is called once per frame
     void Update()
     {
         transform.Translate(-speed, 0, 0);
     }
 }

It's basically the same thing except the way the x direction of the cannon moves is opposite to the other.

I have multiple bullet spawns which are empty game objects. They are called:

  • CannonSpawnsLeft - which is a parent to many of the actual empty game object called spawn(1,2,3 blah blah) and they are all tagged LeftBulletSpawn

  • CannonSpawnsRight- which is a parent to many of the actual empty game object called spawn(1,2,3 blah blah) and they are all tagged RightBulletSpawn

Here is the shoot script for the parent CannonSpawnsLeft and CannonSpawnsRight:

 using UnityEngine;
 using System.Collections;
 
 public class ShootAllCannons : MonoBehaviour
 {
 
 
     #region BulletPrefabVariables
     //Assign the bullet into the prefab. One for the left and one for the right.
     public GameObject LeftCannonBullet;
     public GameObject RightCannonBullet;
     
     //apply a delay time for the bullets to prevent rapid fire
     public float delayTime = 3f;
 
     //a counter to check the current time before it is reset (see the functions below for the reset)
     private float counter = 0;
 
     //stored time variable to destroy the instantiated bullet after being spawned
     public float destroyTime = 2.0f;
 
    
 
     #endregion
 
     void Start () 
     {
     
     }
     
     // Update is called once per frame
     void Update()
     {
         if (Input.GetKey(KeyCode.Mouse0) && counter > delayTime) //left cannon
         {
             GameObject[] leftCannonSpawns = GameObject.FindGameObjectsWithTag("LeftCannonSpawn");
             
 
             foreach (var _leftBulletSpawnPoint in leftCannonSpawns)
             {
                 var bulletInstance = Instantiate(LeftCannonBullet, transform.position, transform.rotation);
                 Destroy(bulletInstance, destroyTime);
                 //audio.Play();
                 counter = 0;
             } // end for each
             
         } //end if
 
         if (Input.GetKey(KeyCode.Mouse1) && counter > delayTime) //right cannon
         {
             GameObject[] rightCannonSpawns = GameObject.FindGameObjectsWithTag("RightCannonSpawn");
 
             foreach (var _rightBulletSpawnPoint in rightCannonSpawns)
             {
                 var bulletInstance = Instantiate(RightCannonBullet, transform.position, transform.rotation);
                 Destroy(bulletInstance, destroyTime);
                 //audio.Play();
                 counter = 0;
             } // end for each
         } //end if
 
         counter += Time.deltaTime;
 
     } //end update
 }//end class


So now here is the problem. It technically works, all the bullets are being instantated, however they are all shooting out through one of the spawns. This means that if I shoot for the left cannons, all the bullets will be instantiated but it will look like one cannon was shooting all of these. the bullets will look like one bullet because they are all shot at the same time, but my hierarchy shows that there are multiple bullets equal to the amount of spawns.

The same thing happens to the right, but it doesn't spawn in the left spawn point. Again only one right spawnpoint is seen to be used. I know it has to be somewhere in their instantions. Their transform.translate and stuff is only applied to one of the spawns. How can I fix this?

Update: Ok so I figured it out, the transform.position was my fault. It should be directed towards the Instance of the spawn then .transform.position.

But now I have another problem. I have been looking around buoyancy scripts and I can't seem to find one that will benfit my boat. Everytime I use one, my boat will just sink to the bottom or fly super high in the air. Sometimes I get really close but the change in position from the boat's height in the water to the air is too high? (sorry if I sound dumb but do I need to adjust my damping?)

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by VildNinja · Aug 16, 2015 at 09:08 PM

You are referring to the ShootAllCannons transform NOT the transform of _leftBulletSpawnPoint. All your foreach loop is good for is counting the number of leftCannonSpawns. Remember to write _leftBulletSpawnPoint.transform instead of just transform.

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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

AddForce not working on instantiated bullets 1 Answer

Weapon firing and weapon selection mechanism for C# game 0 Answers

how to disable instantiating projectile for the moment when the player jumps and hits the space bar to fire? 2 Answers

Floating weapon + shots aren't moving properly. 1 Answer

trouble with cannons 0 Answers


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