Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 wahajabdulrazzak · Feb 24 at 05:56 PM · random

i need a script for Random movement within specific locations

I have this script for random movement within specific locations but there are 2 problems first, it is for images and i want it for objects .Second the objects should not take the features of the original card. please can you help me?

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class randpics : MonoBehaviour { public const int gridRows = 2; public const int gridCols = 3; public const float offsetX = 1.5f; public const float offsetY = 1.5f;

 [SerializeField] private MainCard originalCard;
 [SerializeField] private GameObject[] images;

 private void Start()
 {
     Vector3 startPos = originalCard.transform.position; //The position of the first card. All other cards are offset from here.

     int[] numbers = { 0, 1, 2, 3, 4, 5};
     numbers = ShuffleArray(numbers); //This is a function we will create in a minute!

     for (int i = 0; i < gridCols; i++)
     {
         for (int j = 0; j < gridRows; j++)
         {

             MainCard card;
             if (i == 0 && j == 0)
             {
                 card = originalCard;
             }
             else
             {
                 card = Instantiate(originalCard) as MainCard;
             }

             int index = j * gridCols + i;
             int id = numbers[index];

         

             float posX = (offsetX * i) + startPos.x;
             float posY = (offsetY * j) + startPos.y;
             card.transform.position = new Vector3(posX, posY, startPos.z);
         }
     }
 }

 private int[] ShuffleArray(int[] numbers)
 {
     int[] newArray = numbers.Clone() as int[];
     for (int i = 0; i < newArray.Length; i++)
     {
         int tmp = newArray[i];
         int r = Random.Range(i, newArray.Length);
         newArray[i] = newArray[r];
         newArray[r] = tmp;
     }
     return newArray;
 }

}

Comment
Add comment · Show 1
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 wahajabdulrazzak · Feb 26 at 05:06 PM 0
Share

Thank you for your answer but it doesn't work like I want ..please can you help me to make a script which allowed to 6 images each one has different script to change their position each time I press play within 6 specific position and each image appear one time only.....

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by heinrichs81 · Feb 24 at 10:50 PM

Hi there, I need to say this is very unspecific, because the script you added here do the things that it want to do alone. The questions is therefore to you "where do you need a random movement in a specific location"? Please note, that your script "randpics" is not added to the "Code Sample" area completely, what it makes hard to understand.

  1. To you first problem: It is not for images. You get Objects especially an array of GameObjects that is called images

  2. To the second problem: Please note line 6: the reason why the features taken from the "Original Card" is because you take it from the MainCard variable called originalCard. If you want to Change that, you can change the Objects referenced to MainCard object. Depending on the case what you want to do you can also change the line 6 completely to i.e.,

      //before change
         Vector3 startPos = originalCard.transform.position;
         
         
         //exchange this line with
         // this creates an Vector3 with random numbers for x between 0 and 1
         // for y between 2 and 3 and for z between 1 and 2
         Vector3 startPos = new Vector3(Random.Range(0, 1.0f), Random.Range(2.0f, 3.0f), Random.Range(1.0f, 2.0f));
    
    
    

I hope it makes it a little bit more understandable what your script do. If you have more or other questions maybe we can try to find an answer.

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 wahajabdulrazzak · Feb 25 at 11:53 AM

Thanks for your answer, but it didn't solve the problem...

Can you please give me a script to change the position of the images within specific positions So that each image appears once and changes its position each time with the start of the game.?

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

141 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

Related Questions

random movement 2d? 1 Answer

Random, 3d sound. 1 Answer

how to check for GameObject is null in array with random 1 Answer

Random Player Respawn Points 3 Answers

Whack-A-Mole Issue 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