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 /
  • Help Room /
avatar image
0
Question by Uair · Mar 03, 2016 at 04:59 PM · gameobjectinstantiatequaternionrigidbody2drigidbody.velocity

Trying to get objects to instantiate and move at different angles

I'm trying to make it so when you click left, three balls will fire from the sides, each having a slightly different angle to provide a bit of range. It's a 2D pirate ship game, hence the balls shoot from the sides instead of the front. But even though I was certain I got most of it right, they all merge into the same location. Here's the entire code, please take your time to look at it and maybe alter it a bit.

 using UnityEngine;
 using System.Collections;
 
 public class player_canon_ball : MonoBehaviour {
     public GameObject canonballPrefab;
     public float fireDelay = 0.25f;
     float cooldownTimerPort = 0f;
     float cooldownTimerStarboard = 0f;
     float fireSpeed = 5;
     void Update()
     {
         cooldownTimerPort -= Time.deltaTime;
         cooldownTimerStarboard -= Time.deltaTime;
         if (Input.GetButton("Fire1") && cooldownTimerPort <= 0)
         {
             Debug.Log("Fire Port!");
             cooldownTimerPort = fireDelay;
             GameObject canonball1 = Instantiate(canonballPrefab, transform.position, Quaternion.Euler(0,-90,0)) as GameObject;
             GameObject canonball2 = Instantiate(canonballPrefab, transform.position, Quaternion.Euler(0, -60, 0)) as GameObject;
             GameObject canonball3 = Instantiate(canonballPrefab, transform.position, Quaternion.Euler(0, -120, 0)) as GameObject;
             Rigidbody2D crb1 = canonball1.GetComponent<Rigidbody2D>();
             Rigidbody2D crb2 = canonball2.GetComponent<Rigidbody2D>();
             Rigidbody2D crb3 = canonball3.GetComponent<Rigidbody2D>();
             crb1.velocity = canonball1.transform.forward * fireSpeed;
             crb2.velocity = canonball2.transform.forward * fireSpeed;
             crb3.velocity = canonball3.transform.forward * fireSpeed;
         }
         if (Input.GetButton("Fire2") && cooldownTimerStarboard <= 0)
         {
             Debug.Log("Fire Starboard!");
             cooldownTimerStarboard = fireDelay;
             Vector2 canon4 = new Vector2(transform.position.x, transform.position.y);
             Vector2 canon5 = new Vector2(transform.position.x, transform.position.y);
             Vector2 canon6 = new Vector2(transform.position.x, transform.position.y);
             GameObject canonball4 = Instantiate(canonballPrefab, canon4, Quaternion.Euler(0, 90, 0)) as GameObject;
             GameObject canonball5 = Instantiate(canonballPrefab, canon5, Quaternion.Euler(0, 120, 0)) as GameObject;
             GameObject canonball6 = Instantiate(canonballPrefab, canon6, Quaternion.Euler(0, 60, 0)) as GameObject;
             Rigidbody2D crb4 = canonball4.GetComponent<Rigidbody2D>();
             Rigidbody2D crb5 = canonball5.GetComponent<Rigidbody2D>();
             Rigidbody2D crb6 = canonball6.GetComponent<Rigidbody2D>();
             crb4.velocity = canonball4.transform.forward * fireSpeed;
             crb5.velocity = canonball5.transform.forward * fireSpeed;
             crb6.velocity = canonball6.transform.forward * fireSpeed;
         }
     }
 }

Take as much time as you need, I'm just making this for fun right now, no deadlines or anything.

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
0
Best Answer

Answer by Uair · Mar 03, 2016 at 05:11 PM

Fixed it! I had to fire right or left instead of forward. Just change "crb#.velocity = canonball#.transform.forward fireSpeed" to " "crb#.velocity = canonball#.transform.right fireSpeed"

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

58 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

Related Questions

Instantiate differents objects [C#] 1 Answer

Having trouble with Instantiate 0 Answers

Breaking Object by Destroying it and Instantiating Broken Parts problem, help! 2 Answers

LevelGenerator after Checkpoint Respawn Not Aligning Tiles Properly 1 Answer

instantiate prefab at every gameobject with tag 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