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 Nezix · Oct 07, 2014 at 03:59 AM · collisiondirectionangle

Instantiate at a right angle from collision impact

Hello, I'm trying to instantiate two prefabs at a right angle to the where the collider hits a target.

Background: I'm making my own version of Asteroids in Unity and when a laser hits a asteroid and the asteroid splits, I want it to split to either side of the impact point.

For example, a laser hits an asteroid:

 --> O

I would instantiate a prefab above and below the asteroid because that's at 90 degrees to the left and right of the laser's impact point:

     x
 --> O
     x


     public GameObject splitPrefab;

     void OnCollisionEnter2D(Collision2D coll)
     {
          Quaternion randomRotation;
          Vector3 newPosition;

          for (int i = 0; i < 2; i++)
          {
               // The position of the circle
               newPosition = gameObject.transform.position;

               // the Collision2D will give us information about the arrow
             
               if (i == 0)
               {
                    // rotate left 90 degrees
               }
               else
               {
                    // rotate right 90 degrees
               }
                 
     
               Instantiate(splitPrefab, newPosition, Quaternion.identity);
           }
     }

I'm not sure what sort of math I need here to find the degree difference and then apply that to my newPosition with a distance for each prefab. The solution should work regardless of what direction the laser comes from; for example a laser could hit the same point on a asteroid but it could come directly towards the circle or be a glancing blow. I think in both situations it should be the same. The asteroid will likely be rotating and thus not stationary in that way.The distance between the asteroids center and the new prefab center would likely be a public variable that would depend on the game. This only needs to work in 2D.

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

1 Reply

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

Answer by robertbu · Oct 07, 2014 at 04:30 AM

There are two parts to your problem, figuring out what you want to use for the incoming vector, and finding the two perpendicular positions. The second half is the easy part. Given this code, I'm not sure what you want to use for the incoming vector. You might want to use the Collision2D.relativeVelocity. You maight want to use a vector between the center of the hitting object and the hit point. Reading between the lines for what you want to do, the hit.normal might be a good choice. Or maybe you save the pre-collision velocity of the projectile and use that. Assuming you have that and a you have the contact point from the collision, you can do:

  Quaternion q = Quaternion.AngleAxis(90.0f, Vector3.forward);
  Vector3 v = q * (incoming.normalized * dist);
  pos1 = contactPoint + v;
  pos2 = contactPoint - v;

An alternate solution to use a Quaternion to rotate the vector, would be to use Vector3.Cross();

  Vector3 v = Vector3.Cross(incoming, Vector3.forward);
  v.Normalize() * dist;
  pos1 = contactPoint + v;
  pos2 = contactPoint - v;

'dist' is the distance away along the perpendicular you want to place the point.

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 Nezix · Oct 07, 2014 at 11:01 PM 0
Share

Thank you I got it to work with the Vector3.Cross example.

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

2 People are following this question.

avatar image avatar image

Related Questions

Detecting when an object enters a collider from the top? 1 Answer

Add angle to Vector3 1 Answer

[2D] Angle to Vector conversion 1 Answer

Get angle to retarget collision object. 1 Answer

Slerp look at direction not working... 3 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