• 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 n3wb13 · Jul 06, 2018 at 08:00 AM · rotationrandomspawning

How to generate a random vector3 with the same z distance relative to a map with rotation?

How can I randomize the spawn location(x) of my gameobject while maintaining it's z distance from the map? Doing a transform.position.z won't do since the spawner and the map have a rotation.

 Vector3 spawnLoc = new Vector3(Random.Range(minX, maxX), transform.position.y, ?????);

alt text

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

3 Replies

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

Answer by n3wb13 · Jul 06, 2018 at 10:18 AM

Hi guys solved it myself this is what I did.

     public float minX = -7.45f;
     public float maxX = -1.45f;
     float prevX;
     float prevZ;
 
     void Start () {
         prevX = transform.position.x;
         prevZ = transform.position.z;        
     }
 
  void Generate() {
 
         float newX = Random.Range(minX, maxX);
         float newZ = ((prevX - newX) - prevZ) * -1;
 
         prevX = newX;
         prevZ = newZ;
 
         Vector3 spawnLoc = new Vector3(newX, transform.position.y, newZ);
     }
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 madks13 · Jul 06, 2018 at 08:28 AM

Does that mean your map is moving and rotating? Why not use relative positioning? If your spawned object is set as a child of the map, you won't need to know the rotation or position of the map. Just the relative position of your object map wise.

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 n3wb13 · Jul 06, 2018 at 08:39 AM 0
Share

@madks13 sorry but I don't get it. BTW the map is not moving or rotating.

avatar image
0

Answer by Zodiarc · Jul 06, 2018 at 08:42 AM

Why don't you keep the map and spawner rotation at 0 and just rotate the camera? Same visual effect, easier maths.

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 n3wb13 · Jul 06, 2018 at 08:50 AM 0
Share

I already consider that but as a last resort. Since I would need to change a handful of scripts and existing map design.

avatar image Zodiarc n3wb13 · Jul 06, 2018 at 08:55 AM 0
Share

I think what you need is using maths specific for Polar coordinate systems

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

120 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

Related Questions

Flip over an object (smooth transition) 3 Answers

Rotate within range 1 Answer

How can i make my 2D character not toppling over 0 Answers

Pointing object at mouse using raycast only works when object is center screen. 2 Answers

Unassigned reference exception after rewarded ad 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges