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 AnScience · Feb 09, 2020 at 06:22 PM · positionvector3random

Spawn object at random position in front of player

I want to spawn cubes in front of the player at random positions in front of the player. So these positions should be constrained so the player is able to touch/interact with them. This works perfectly when facing the Z-axis, however the problem I'm having is that whenever I rotate the player looking at the X-axis. Then cubes are spawned on top of the player rather then having the depth offset. What do I have to do in order to generate cubes on random, constrained positions in front of the player?

     /**
      * Calculate a random, constrained position in front of the player.
      * Constraints:
      * x: -1.0f & 1.0f from the player
      * y: not below 0 or player height
      * z: random depth from the player
      */
     private Vector3 GetRandomPosition()
     {
         var player = PlayerManager.Instance.player.transform;
         Debug.Log("Transform forward: " + player.transform.forward);
 
         var spawnPos = player.position;
         spawnPos.x += player.transform.right.x * Random.Range(-1.0f, 1.0f);
         spawnPos.y = Random.Range(0.0f, 2.0f);
         spawnPos.z += player.transform.forward.z * Random.Range(0.5f, 1.0f);
 
         return spawnPos;
     } 

Comment
Add comment · Show 2
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 sacredgeometry · Feb 10, 2020 at 07:52 AM 1
Share

You have a local and global space when you rotate an object its local rotational axis get offset from the global ones.

Just make sure you are using the correct coordinate space/ transform when you are spawning items.

avatar image AnScience · Feb 10, 2020 at 10:26 AM 0
Share

hi @sacredgeometry Thanks for your reply. Can you give some more information on how I would do that. At the moment I've fixed it by creating a random offset from my forward direction. See my answer below.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by AnScience · Feb 10, 2020 at 10:28 AM

At the moment I managed to fix my issue by calculating a random vector in world space and adding it to my forward offset:

 /**
  * Calculate a random, constrained position in front of the player.
  * Constraints:
  * x:
  * y: not below 0 or player height
  * z: Not 
  */
 private Vector3 GetRandomPosition()
 {

     var player = PlayerManager.Instance.player;
     var distance  = 1.0f;
     var tolerance = 0.5f;
     var offset= player.transform.forward * distance;
     var position = offset + new Vector3(Random.Range(-tolerance, tolerance), Random.Range(0f, 2.0f), Random.Range(-tolerance, tolerance));

     return position;
 }


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

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

Related Questions

Vector3 not receiving coordinates 1 Answer

Circle inside Circle 1 Answer

Randomly position Instantiated GameObject's 1 Answer

Vector3.Lerp moves other characters over network to (0, 0, 0) 1 Answer

spawn muzzle flash with random rotation? 2 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