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 21, 2016 at 03:08 PM · c#movementarraydirectionrandom.range

Create an array that chooses between four random numbers (C#)

Hello my fellow Unities! I'm making a somewhat pong clone and I'm trying to make it so that when a ball spawns it will fly off "Up right", "Up left", "Down left" and so on. The angles would be 45, 135, 225, 315. Now I have created an array and so that the ball will fly off.

But the problem is that the ball just flys off in every random direction, 90, 180 degress and so on. How can I make it like this: alt text

Here's my code:

 using UnityEngine;
 using System.Collections;
 
 public class GrowingEnemyScript : MonoBehaviour 
 {
     //Make the enemy bounce 
     Rigidbody2D bouncing;
 
     public int[] angleArray;
 
     //Speed on the ball
     float speed = 8;
 
 
     Vector3 velocity;
     
     void Start() 
     {
         //Create 4 angles for the ball to 
         angleArray = new int[4];
 
         //Array values
         angleArray[0] = 45;
         angleArray[1] = 135;
         angleArray[2] = 225;
         angleArray[3] = 315;
 
         //transform.position = Vector3.zero;
         transform.rotation = Quaternion.identity;
         
         // Random range where the ball is supposed to fly
         float angle = (Random.Range(0, angleArray.Length));
         
         // Calculate velocity for ball
         velocity = new Vector2 (Mathf.Cos (angle), Mathf.Sin(angle));
         
         // Get the ball moving! 
         this.GetComponent<Rigidbody2D> ().velocity = velocity * speed;
 
     }

}

exploding-ball-problem3.png (17.7 kB)
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

3 Replies

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

Answer by Waka-Takaki · Apr 21, 2016 at 03:58 PM

You need to perform Random.range with an int, not a float.

 int selection = Random.Range(0, angleArray.Length - 1);
 int angle = angleArray[selection];

Then apply your angle to the velocity using callen's method below

Comment
Add comment · Show 4 · 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 Dave-Carlile · Apr 21, 2016 at 04:11 PM 0
Share

The float data type here makes no difference to the problem at hand. In fact, $$anonymous$$athf.Cos requires passing a float so if angle is an int then it will be implicitly type cast to a float anyway.

avatar image Waka-Takaki · Apr 21, 2016 at 04:38 PM 1
Share

Does not Random.Range give a float value if passed to float? So it would give a float between 0 and 4 which would not actually be a value that could be passed to the angleArray. The angle variable above could be cast to a float for the mathf function

avatar image Dave-Carlile Waka-Takaki · Apr 21, 2016 at 05:52 PM 0
Share

:$ Oh, yeah, that's what I get for not paying close enough attention. OP isn't using angle as an index into angleArray at all.

avatar image Internetman · Apr 21, 2016 at 06:47 PM 0
Share

That did it! You guys really are masters at what you do!

avatar image
1

Answer by Dave-Carlile · Apr 21, 2016 at 03:16 PM

Cos and Sin expect an angle in radians but you're passing in degrees. You can convert from degrees to radians by multiplying degrees by Mathf.Deg2Rad.

Another option would be to just store the vector in your array directly so you don't need to do the trig.

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 callen · Apr 21, 2016 at 03:16 PM

This should fix it:

velocity = new Vector2 (Mathf.Cos (angle*Mathf.Deg2Rad), Mathf.Sin(angle*Mathf.Deg2Rad));

Cos and Sin use radians, but youre giving it degrees. The built-in consts Deg2Rad and Rad2Deg are there for this reason.

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 21, 2016 at 03:34 PM 0
Share

I think this outta do it, I tried do it but, I think there's something wrong with where I set the random.range since it still makes the ball go in direction which I have not set it to be.

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

135 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

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

Tracking HTC Vive Controller Motion Direction? 1 Answer

Moving object along with raycast 1 Answer

How do I turn 1 objects rotation into another objects movement direction? 1 Answer

Trying to move my gameobeject in a certain direction (2D, C#) 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