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 Kalevala17 · Oct 23, 2018 at 04:50 PM · random.rangerandomspawning

Random.Range isn't returning whole numbers

I'm attempting to make a simple simulation of grass growing, but for the life of me I cannot get it to work! My latest problem is that I cannot get the grass to spawn in a random location relative to the spawned parent in a whole number. So it will spawn a tuft (cube, really) of grass near the parent, but I want it to be within a whole number's distance away e.g. 1,2,3 instead of 1.1, 1.2, 1.3 Here's the code:

using System.Collections; using System.Collections.Generic; using UnityEngine; using Random = UnityEngine.Random;

public class GrassScript : MonoBehaviour { public GameObject Grass; public GameObject GrassCore; public Transform GrassPosition;

 // Update is called once per frame
 void Update () {
     if (Input.GetKeyDown("d"))
     {
         GameObject GrassPrefab = Instantiate(Grass) as GameObject;
         GrassPrefab.name = "Grass";
         GrassPrefab.transform.position = new Vector3(Random.Range((GrassPosition.position.x)-2, (GrassPosition.position.x)+3), GrassPosition.position.y, Random.Range((GrassPosition.position.z) - 2, (GrassPosition.position.z) + 3));
        

     }
 }

}

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 Hellium · Oct 23, 2018 at 05:28 PM

Random.Range does not return a whole number if you don't provide integers. Thus, you will have to provide the function to get an integer from the floating position. Either round, floor or ceil the values:

 Vector3 position = GrassPosition.position ;
 
 position.x = Random.Range(
     Mathf.CeilToInt(GrassPosition.position.x) - 2, // You can also use Mathf.RoundToInt or another function
     Mathf.FloorToInt(GrassPosition.position.x) + 3
 ) ;
 position.z = Random.Range(
     Mathf.CeilToInt(GrassPosition.position.z) - 2,
     Mathf.FloorToInt(GrassPosition.position.z) + 3)
 ) ;
 
 GrassPrefab.transform.position = position ;




Comment
Add comment · Show 2 · 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 Kalevala17 · Oct 23, 2018 at 06:23 PM 0
Share

Having another unforseen issue though: Now the grass spawns awesomely (again, thanks!) but I want it to run a check to see if grass is already at those coordinates and if it is, to delete the grass already there and replace it, so that there is only ever one 'tuft' in any given location. I'm thinking to use some kind of boolean checker, but I don't really know since I'm so new to this.

avatar image Hellium Kalevala17 · Oct 24, 2018 at 08:41 AM 0
Share

I haven't tested the following code, but you can give a try:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class GrassScript : $$anonymous$$onoBehaviour
 {
     public GameObject Grass;
     public GameObject GrassCore;
     public Transform GrassPosition;
 
     private List<GameObject> grassInstances;
 
     void Start ()
     {
         grassInstances = new List<GameObject>(128);
     }
 
     void Update ()
     {
         if ( Input.Get$$anonymous$$eyDown("d") )
             AddGrassInstance( InstantiateGrass() ) ;
     }
 
     private GameObject InstantiateGrass()
     {
         GameObject grassInstance = Instantiate(Grass) as GameObject;
         grassInstance.name = "Grass";
         Vector3 position = GrassPosition.position ;
  
         position.x = Random.Range(
             $$anonymous$$athf.CeilToInt( GrassPosition.position.x ) - 2,
             $$anonymous$$athf.FloorToInt( GrassPosition.position.x ) + 3
         ) ;
         position.z = Random.Range(
             $$anonymous$$athf.CeilToInt( GrassPosition.position.z ) - 2,
             $$anonymous$$athf.FloorToInt( GrassPosition.position.z ) + 3
         ) ;
         
         grassInstance.transform.position = position ;
         return grassInstance;
     }
 
     private void AddGrassInstance( GameObject grassInstance )
     {
         int grassIndex = GetGrassIndexAtPosition( grassInstance.transform.position ) ;
         if( grassIndex >= 0 )
             grassInstances[i] = grassInstance ;
         else
             grassInstances.Add( grassInstance );
     }
 
     private int GetGrassIndexAtPosition( Vector3 position )
     {
         int grassCount = grassInstances.Count;
         for( int i = 0 ; i < grassCount ; ++i )
             if( (grassInstances[i].transform.position - position ).sqrDistance < 0.2f )
                 return i ;
 
         return -1 ;
     }
 }

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

157 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 avatar image avatar image avatar image avatar image 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

Randomly Spawn Outside The Camera Field of View 1 Answer

How to spawn objects from the right side outside of view range? 0 Answers

calling random numbers repeatedly with random.Range 1 Answer

Two objects sharing a script should spawn different random objects, but instead they spawn the same random object. 1 Answer

continously spanws and overlaps 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