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 Internetman · Apr 25, 2016 at 04:24 PM · c#arraydirectionvalueinitialize

Assign gameobject value from an array (C#)

I know this probably a simple question but I have a problem with one of my enemies in my game. I've created a script that I set on the prefab of this enemy, but the problem is that I spawn out four enemies of the same sort, but I want these to fly in there own direction with the help of an assigned array that set the value that they should fly in.

But how can I set the value and make the first ball called "explodingball1" go in the 45 angle direction, then for explodingball2 I want to set the direction 135, explodingball 3 225 direction etc..

Note that I currently use a Random.range to set the direction of the 4 balls, but as I said they fly off in on of these four random directions that I set.

Here's the code:

  using UnityEngine;
    using System.Collections;
    
    public class EnemyExplodeBallScript : MonoBehaviour 
    {
        Rigidbody2D bouncing;
        public float speed = 30;
    
        public float timer = 3.0f;
        
        Vector3 velocity;
    
        float[] angleArray;
    
        public GameObject explodingBall1;
        public GameObject explodingBall2;
        public GameObject explodingBall3;
        public GameObject explodingBall4;
    
        
        void Start() 
        {
            
    
            if(GameObject.Find("Enemy_explode(Clone)").tag == "EnemyExplodeMiddle")
            {
                //Create 4 angles for the ball to 
                angleArray = new float[4];
                
                //Array values
                angleArray[0] = 45;
                angleArray[1] = 135;
                angleArray[2] = 225;
                angleArray[3] = 315;
                
                transform.rotation = Quaternion.identity;
                
                
    // Random range where the ball is supposed to fly within the array
                int selection = Random.Range(0, angleArray.Length);
                float angle = angleArray[selection];
    
                for(int i = 0; i < angleArray.Length; i++)
                {
                               **//Should I use a for loop?** 
                }
                
                // Calculate velocity for ball
                velocity = new Vector2 (Mathf.Cos (angle*Mathf.Deg2Rad), Mathf.Sin(angle*Mathf.Deg2Rad));;
                
                // Get the ball moving! 
                this.GetComponent<Rigidbody2D> ().velocity = velocity * speed;
            }
    
            
        }
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 Kazuva · Apr 25, 2016 at 04:40 PM

i'm not quite sure what's the issue BUT, if the issue that they all go in the same direction you could set

 static int ballCounter = 0;

to count balls and

 float angle = angleArray[ballCounter];
 ballCounter += 1;


to get the balls to go in a different angles

and if you need to summon more bals than there is angles in your array just

 ballCounter = 0;







Comment
Add comment · Show 1 · 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 Internetman · Apr 25, 2016 at 05:05 PM 0
Share

This made it work! Thank you my friend!

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

134 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to let multiple values contribute two one value constantly 1 Answer

Assigning value stored in an array with get{} using cSharp 1 Answer

how to initialize multidimensional jagged array 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