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 missypooh · Mar 13, 2012 at 03:39 PM · networkingmultiplayernetwork.instantiate

Randomize the position of the player

I would like to ask how to randomise the position of the player once the network is loaded. The code snippet below is what i have done to randomise the position of the player but it doesn't seem to work. All the players are spawn at the same position. Can i know what wrong with the code?

 function OnNetworkLoadedLevel()
     {
         // Randomize starting location
         var spawnpoints : GameObject[] = GameObject.FindGameObjectsWithTag ("Spawnpoint");
         Debug.Log("spawns: "+spawnpoints.length);
         
         var spawnpoint : Transform = spawnpoints[Random.Range(0, spawnpoints.length)].transform;
         var newTrans : Transform = Network.Instantiate(playerPrefab,spawnpoint.position, spawnpoint.rotation, 0);
     }
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

Answer by J8Ptt · Mar 13, 2012 at 05:26 PM

Random.Range() returns a float value, but you are using it as an array index (which is an int value). Maybe that's the problem? Try rounding the output of random:

var spawnpoint : Transform = spawnpoints[Mathf.RoundToInt(Random.Range(0, spawpoints.length))].transform;

Does it work?

Comment
Add comment · Show 12 · 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 missypooh · Mar 13, 2012 at 06:08 PM 0
Share

No.. It still spawn at the same position. anyway i got an error for this line: var newTrans : Transform = Network.Instantiate(playerPrefab,spawnpoint.position, spawnpoint.rotation, 0);

InvalidCastException: Cannot cast from source type to destination type. GameSetup.OnNetworkLoadedLevel () (at Assets/Scripts/$$anonymous$$ultiPlayerScripts/GameSetup.js:144) UnityEngine.GameObject:Send$$anonymous$$essage(String, Send$$anonymous$$essageOptions) GameSetup:Awake() (at Assets/Scripts/$$anonymous$$ultiPlayerScripts/GameSetup.js:44)

avatar image J8Ptt · Mar 13, 2012 at 06:37 PM 0
Share

That's a casting error. Try explicitly cast the result of the "Instantiate" to Transform:

var newTrans : Transform = (Transform) Network.Instantiate(playerPrefab,spawnpoint.position, spawnpoint.rotation, 0);

About the random, are you sure that the "spawpoints" array has something? I can't see anything wrong with the code, so I suggest you double-check if your spawn points are correctly labeled.

avatar image Kleptomaniac · Mar 13, 2012 at 08:18 PM 0
Share

I believe J8$$anonymous$$t is probably right that the FindGameObjectsWithTag is not actually returning anything to the array ... in your Debug.Log, are you getting the .length you expect? If not, like J8$$anonymous$$t said, you've probably forgotten to tag you spawn points with "Spawnpoint".

avatar image simubrett · Mar 13, 2012 at 09:55 PM 3
Share

As a side note, J8Ptt, Random.Range returns an int when given int parameters. I use it for returning random array elements all the time.

avatar image simubrett · Mar 13, 2012 at 09:58 PM 0
Share

If missypooh's code is spawning everyone at the same position, with the code above, it means there's at least one Spawnpoint. I'd guess there isn't more than one, though.

Show more comments
avatar image
0

Answer by syclamoth · Mar 14, 2012 at 02:51 AM

If you want to spawn in a random position, try using something like this:

 // having determined the spawnPoint, get a random point in a circle:
 var randomCircle : Vector2 = Random.insideUnitCircle;
 var spawnOffset : Vector3 = new Vector3(randomCircle.x, 0, randomCircle.y);

 var newTrans : Transform = Network.Instantiate(playerPrefab,spawnpoint.position + spawnOffset, spawnpoint.rotation, 0);

Otherwise, if you want to have a large number of spawnPoints, you can either collect them with the use of a common tag, and use the code that you posted in your question (make sure that all the transforms that you are using as spawn points have the "Spawnpoint" tag), or you can manually define them by dragging them into an array in the inspector:

 // right at the top!
 public var spawnPoints : Transform[];

 // then when you go to use it:

 var spawnPoint : Transform = spawnPoints[Random.Range(0, spawnPoints.Length)];

If you want to use both, there's no reason why you can't apply the position randomisation after having chosen a spawnPoint!

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 missypooh · Mar 14, 2012 at 01:26 PM 0
Share

hi, i try using the one on spawnPoints: Transform; But my player fall infinitely..

This is what i do.. correct me if i am wrong.

  1. First i create 5 spawnpoints.

  2. Then in the inspector, i initialize on the spawnpoints.

  3. The script is attached to the empty gameObject not on the spawnpoint. So is there anything wrong?

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity networking tutorial? 6 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

what should be considered first in multiplayer game 1 Answer

Set unity process as server and client? 0 Answers

Multiplayer LAN game? 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