• 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 KristofferH · Mar 31, 2016 at 07:34 PM · rotationpositionmathspawning

Calcultate spawn position based on spawn volume's rotation

I have created a empty GameObject and added a BoxCollider to it. I'm using the size of the collider to define a spawning volume for drones in the game. It works nice, but it doesn't take into account the rotation for the GameObject/BoxCollider. So I need some sort of fancy math equation here to calculate the spawn position for the drones based on the spawn volumes rotation in the scene.

Here is the code so far:

 using UnityEngine;
 using System.Collections;
 
 public class SpawnArea : MonoBehaviour
 {
     public GameObject Drone;
     public float TimeLeft;
     public float MinTime = 1.0f;
     public float MaxTime = 2.0f;
 
     void Start ()
     {
         TimeLeft = Random.Range(MinTime, MaxTime);
     }
     
     void Update ()
     {
         TimeLeft -= Time.deltaTime;
 
         if (TimeLeft < 0)
         {
             BoxCollider volume = GetComponent<BoxCollider>();
             Vector3 size = volume.size;
             Vector3 offset = volume.center;
 
             Vector3 pos;
             pos.x = Random.Range(transform.position.x + offset.x - size.x * transform.localScale.x / 2, transform.position.x + offset.x + size.x * transform.localScale.x / 2);
             pos.y = Random.Range(transform.position.y + offset.y - size.y * transform.localScale.y / 2, transform.position.y + offset.y + size.y * transform.localScale.y / 2);
             pos.z = Random.Range(transform.position.z + offset.z - size.z * transform.localScale.z / 2, transform.position.z + offset.z + size.z * transform.localScale.z / 2);
 
             Quaternion rot;
             rot = Quaternion.Euler(transform.rotation.eulerAngles.x - 180, transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z - 90);
 
             Instantiate(Drone, pos, rot);
             TimeLeft = Random.Range(MinTime, MaxTime);
         }
     }
 }

Any ideas on how to fix this? :)

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
0
Best Answer

Answer by KristofferH · Apr 03, 2016 at 08:38 PM

I found a solution to this problem in another thread. Here is the new code:

 using UnityEngine;
 using System.Collections;
 
 public class SpawnArea : MonoBehaviour
 {
     public GameObject Drone;
     public float TimeLeft;
     public float MinTime = 1.0f;
     public float MaxTime = 2.0f;
     
     void Start ()
     {
         TimeLeft = Random.Range(MinTime, MaxTime);
     }
     
     void Update ()
     {
         TimeLeft -= Time.deltaTime;
 
         if (TimeLeft < 0)
         {
             BoxCollider volume = GetComponent<BoxCollider>();
             Vector3 size = volume.size;
             Vector3 offset = volume.center;
 
             Vector3 pos;
             pos.x = Random.Range(transform.localPosition.x + offset.x - size.x * transform.localScale.x / 2, transform.localPosition.x + offset.x + size.x * transform.localScale.x / 2);
             pos.y = Random.Range(transform.localPosition.y + offset.y - size.y * transform.localScale.y / 2, transform.localPosition.y + offset.y + size.y * transform.localScale.y / 2);
             pos.z = Random.Range(transform.localPosition.z + offset.z - size.z * transform.localScale.z / 2, transform.localPosition.z + offset.z + size.z * transform.localScale.z / 2);
 
             pos = RotatePos(pos, transform.position, transform.rotation.eulerAngles);
 
             Quaternion rot;
             rot = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z);
 
             Instantiate(Drone, pos, rot);
 
             TimeLeft = Random.Range(MinTime, MaxTime);
         }
     }
 
     public Vector3 RotatePos(Vector3 point, Vector3 pivot, Vector3 angles)
     {
         Vector3 dir = point - pivot; // get point direction relative to pivot
         dir = Quaternion.Euler(angles) * dir; // rotate it
         point = dir + pivot; // calculate rotated point
         return point; // return it
     }
 }

And here is the link to the original thread: http://answers.unity3d.com/questions/532297/rotate-a-vector-around-a-certain-point.html

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 phxvyper · Mar 31, 2016 at 07:51 PM

Theres a fancy little function that does all that work for ya:

 Vector3 worldPos = transform.TransformPoint(pos);

Instantiate with worldPos instead of pos. Heres the documentation for this function.

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 KristofferH · Apr 01, 2016 at 07:19 AM 0
Share

Ah, well yes that did take care of the rotation problem, but the positions are now way of ins$$anonymous$$d. So the drones seems to be spawning within the spawn volume but it is like the spawn volume itself is located in a different area. So there seems to be some problem with my calculation of the variable pos.

avatar image phxvyper · Apr 03, 2016 at 09:45 PM 0
Share

@$$anonymous$$ristofferH were you adding the new position to the position of your volume? If you were, this is why it seemed off.

Do TransformPoint on your relative position and treat that as a world position. Dont add it to anything and it will work

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

51 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

Related Questions

Add an offset to the position of an object in front of it 1 Answer

Positions to rotations of a regular GO 2 Answers

Find intersection (with Picture) 2 Answers

Weird offset while Instantiating GameObject 1 Answer

How to write a script to disable position and rotation tracking for VR 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