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
1
Question by Sir Mate · Mar 31, 2012 at 10:08 AM · rotationcollisioninstantiatevector3

Get rotation from hit object

Hey there

I am trying to achieve some things with a projectile:

  1. Destroy on collision -check

  2. Ragdolloze hit object -check

  3. leave the impact effect on object

  • a. instantiate the impact effect -check

  • b. at the hitpoint -check

  • c. align impact effect with the surface - X

It is frustrating ,becouse the position is perfect and calculated from the hit.contacts contact.point,i just cant get the rotation to work.

Here is my code:

 var impactEffect : GameObject;
 
 function Start () { //auto destroy to prevent infinite projectiles
     yield WaitForSeconds(2.0);
     Destroy (gameObject);
 }
 function OnCollisionEnter ( hit : Collision){
     Destroy (gameObject);
     Debug.Log(hit.gameObject);
 
     for (var contact : ContactPoint in hit.contacts) {
         var ieffect = Instantiate (impactEffect, contact.point,transform.rotation); //rotation is a problem
         ieffect.transform.parent = hit.gameObject.transform;
     }
     hit.gameObject.rigidbody.isKinematic = false; //ragdollize hit object
     hit.gameObject.SendMessageUpwards("Ragdollize");
 }

I tried using this :

 var hitRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);

But i just dont know how to get this to work.

Any help would be greatly appriciated.

Thanks

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

Answer by Bunny83 · Mar 31, 2012 at 10:44 AM

The easiest way is to setup your hit-effect prefab looks along the forward axis (the blue z axis). That way you can use LookRotation with hit.normal

Something like that:

 Instantiate (impactEffect, contact.point, Quaternion.LookRotation(contact.normal));

Keep in mind that in this case the z-axis will be aligned to your hit normal

Comment
Add comment · Show 7 · 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 Sir Mate · Mar 31, 2012 at 11:13 AM 0
Share

Thanks for the quick reply! Could you please show me how would you exactly implement this into the above script? Im new to this stuff, and changing it to your solution wont spawn it at all.

I suspect i have to give a variable or something, but i dont know how.

avatar image aldonaletto · Mar 31, 2012 at 11:28 AM 0
Share

You could use only hit.contacts[0] - replace the whole for loop with this code:

 ...
 var contact : ContactPoint = hit.contacts[0];
 var ieffect = Instantiate (impactEffect, contact.point, Quaternion.LookRotation(contact.normal));
 ieffect.transform.parent = hit.gameObject.transform;
 ...

But remember what @Bunny83 said: your particle effect must emit in the Z direction, because LookRotation will align this direction to the surface normal.

avatar image Sir Mate · Mar 31, 2012 at 12:04 PM 0
Share

Thanks again ,it worked ,but i think i see now what you meant. The effect is always 90 degrees wrong, but i dont know how to set the axes, could you please help me out on that one aswell? Just rotating it in the prefab doesnt work.

avatar image Bunny83 · Mar 31, 2012 at 07:33 PM 0
Share

Jep, sorry about that. I've just confused Collision and RaycastHit ;)

Sure you can rotate it in the prefab. $$anonymous$$aybe you need to parent the actual effect to an empty gameobject and turn this into a prefab.

It's important that the effect points in the direction of the blue (forward) axis.

avatar image Sir Mate · Apr 01, 2012 at 11:39 AM 0
Share

Yes ,it works perfectly with a particle effect, but im trying to instantiate a mesh (WaterPlane_$$anonymous$$esh as a test),and it always spawns 90° wrong on the X axis. Changing in the prefab wont help :\ I only want this as a tiny effect, like a bullethole which lingers. I couldnt do anything with projectors ,they acter weird, projecting bloody everywhere, so i tried this.

I appriciate all your help. :)

Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Applying force with respect to angle with the ground? 1 Answer

Workaround for Quaternion.eulerAngles 360 Degree Limit? 1 Answer

Turning Vectors.... 2 Answers

Instantiate a rotated object 4 Answers

OnCollisionEnter not being called, between a Rigidbody and a Box Collider 1 Answer


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