Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 thenachotech1113 · Oct 27, 2012 at 02:39 PM · positionrandomturretspawnpoint

randomise spawnpoints position

hey, I`m trying to make it so when turrets run out a spawn point spawns a turret at its position, then it randomizes a new position were the next one will spawn(the order in which that happens doesn't matter) for any wierd reason it spawns one, then moves to an other position(the same one each time) and stays there and spawns all the others right there. so here is the code I`m using.

 #pragma strict
 var player : String;
 var ObjectToDeploy : GameObject;
 var deploied = true;
 var turretCount : float;
 var range1 : Transform;
 var range2 : Transform;
 
 function Update () {
 Debug.Log("turretCount:"+turretCount);
 
     if (turretCount <= 0){
         
         Deploy ();
         
     }
 
 }
 
 
 
 
 
 function Deploy()
 {
     if (deploied){
      Instantiate(ObjectToDeploy, transform.position, transform.rotation);
      deploied = false;
      transform.position.x = Random.Range(range1.position.x, range2.position.x);
      transform.position.x = Random.Range(range1.position.y, range2.position.y);
      }
 }
 
 function TurretCountAdd () {
     turretCount += 1;
     deploied = true; 
 
 }
 function TurretCountReduce () {
     turretCount -= 1;
 }

Thank you all and sorry for any spelling mistakes.

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

2 Replies

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

Answer by phodges · Oct 27, 2012 at 02:49 PM

In the code that you have posted, you are randomly setting the 'x' component of your deployment point's position twice. I expect you want to update x and z. Since the second random selection is based on the 'y' component of your two reference points, I'm also willing to bet that the reason you see no change in position after the first placement is because they are located at the same height.

As mentioned in another answer, your deployment point is hopping around the map and turrets being placed at its location. Is that really what you want? I suppose it could be useful to see where the next turret will appear, before it spawns, but there are other ways to do that. You want some code? Here's some code:

 function Deploy()
 {
     if (deploied){
      Instantiate(ObjectToDeploy, transform.position, transform.rotation);
      deploied = false;
      transform.position.x = Random.Range(range1.position.x, range2.position.x);
      transform.position.z = Random.Range(range1.position.z, range2.position.z);
      }
 }
Comment
Add comment · Show 3 · 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 Montraydavis · Oct 27, 2012 at 03:31 PM 0
Share

It may be his intention, and I could just be misunderstanding that, but, this code still moves the current object, and not the newly instantiated objects position.

The code I posted will get that job done with only 2 lines.

avatar image thenachotech1113 · Nov 12, 2012 at 05:12 PM 0
Share

awesome thanks

avatar image phodges · Nov 12, 2012 at 05:25 PM 0
Share

Does this code do what you wanted? As mentioned, hopping your spawning object around the map is a bit strange. I can delete this answer to keep things clean if not.

avatar image
1

Answer by Montraydavis · Oct 27, 2012 at 02:49 PM

Because you are actually not changing the position of the newly instantiated object, but, the old one. Also, You might want to randomize before initiation.

Try this...

function Deploy()

{

var RandomVec3 = Vector3 ( Random.Range ( x, x ), transform.position.y, transform.position.z ) ;

 if (deploied){

  Instantiate(ObjectToDeploy, RandomVec3, transform.rotation);

  deploied = false;

  }

}

function TurretCountAdd () {

 turretCount += 1;

 deploied = true; 

}

function TurretCountReduce () {

 turretCount -= 1;

}

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

11 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

Related Questions

Random Position For GameObject 1 Answer

[Beginner] Using Random.Range in initial game object position 1 Answer

Spawn GameObjects without overlap 1 Answer

Rotating object towards mouse point 1 Answer

I want to change the object position random in five particular position only. 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