Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 danielreyvega · Jul 02, 2011 at 02:48 PM · collisioninstantiateraycast

Raycast bullet impact and instantiating particles

Hi,

I've the following code that works, but in a extrange way! I've tried the same but using OnCollision enter instead the raycast.and works a bit better, but on both, come impacts appear on the oposite side of the object that the bullet colide with.

In raycast the bullet decals works fine. but the particle emiters appear away of the impact point.

Can somebody check this code and tell me what is wrong?

Thanks and sorry for my english!

 var imp_tierra: GameObject;
 var imp_piedra: GameObject;
 var imp_metal: GameObject;
 var imp_sangre: GameObject;
 var impacto : GameObject;
 var dir_frz : Vector3;
 
 function Update(){
     comprueba_colision();
 }
 
 function Start(){
     Destroy(gameObject, 15);
 }
 function comprueba_colision(){
     var direction = transform.TransformDirection(Vector3.forward);
     var hit : RaycastHit;
     dir_frz=direction;
     // Did we hit anything?
     if (Physics.Raycast (transform.position, direction, hit, 2)){
         Debug.Log("Impacto");
         impacta(hit);
     }
     
 }
 function impacta(col : RaycastHit) {
     //Destroy(gameObject);
     
     var instParticulas : GameObject;
     var instImpacto : GameObject;
     if (col.collider.tag=="Enemigo"){
         instParticulas=Instantiate(imp_sangre,col.point,Quaternion.FromToRotation(Vector3.up ,col.normal));
         instParticulas.transform.parent=col.transform;
         instImpacto=Instantiate(impacto,col.point,Quaternion.FromToRotation(Vector3.up ,col.normal));
         instImpacto.transform.parent=col.transform;
         instImpacto.transform.Translate(Vector3.up * 0.01);
         if (col.rigidbody)
         col.rigidbody.AddForceAtPosition(50 * dir_frz, col.point);
         Destroy(gameObject);
     }
     if (col.collider.tag=="Terreno"){
         instParticulas=Instantiate(imp_tierra,col.point,Quaternion.FromToRotation(Vector3.up ,col.normal));
         instImpacto=Instantiate(impacto,col.point,Quaternion.FromToRotation(Vector3.up ,col.normal));
         instImpacto.transform.Translate(Vector3.up * 0.01);
         Destroy(gameObject);
     }
     if (col.collider.tag=="Piedra"){
         instParticulas=Instantiate(imp_piedra,col.point,Quaternion.FromToRotation(Vector3.up ,col.normal));
         instImpacto=Instantiate(impacto,col.point,Quaternion.FromToRotation(Vector3.up ,col.normal));
         instImpacto.transform.Translate(Vector3.up * 0.01);
     }
     if (col.collider.tag=="Metal"){
         instParticulas=Instantiate(imp_metal,col.point,Quaternion.FromToRotation(Vector3.up ,col.normal));
         instImpacto=Instantiate(impacto,col.point,Quaternion.FromToRotation(Vector3.up ,col.normal));
         instImpacto.transform.Translate(Vector3.up * 0.01);
     }
 }
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

Answer by aldonaletto · Jul 02, 2011 at 06:40 PM

I couldn't find anything wrong in your code. In the "enemigo" case, you may have problems with the enemy collider - if it's a capsule collider, the impact point may appear very out of position, since col.point will show the collider's hit point, not the mesh's. The same applies to any object with simple colliders (box, sphere etc.); the terrain and objects with mesh colliders should be ok. The particle direction problem may be caused by objects which were already rotated to the "correct" position when they became prefabs: Instantiate will replace any rotation they previously had by the one you've defined (from Up to Normal). I tested this: I created a bloody particle system with Local Velocity 10 in the Y direction only, and instantiated it at the raycast hit point with the rotation Vector3.up to col.normal, exactly like yours, and the blood splashed in the surface normal direction, as expected. I rotated the prefab 90° around the X axis, but the blood still splashed in the same direction. If your blood prefab originally was emitting in the X direction, for example, you can use Vector3.right instead of Vector3.up.

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 aldonaletto · Jul 02, 2011 at 11:48 PM 1
Share

If you have solid modeled objects like trucks, tanks, buildings etc., you can use meshs colliders - they follow the object format, and collisions are way more precise (but more expensive to the performance). Live creatures like enemies, monsters etc. should use a capsule or sphere collider, because they move parts like arms and legs, and the mesh collider could not be recalculated at every movement. You can child objects to the enemy to combine their colliders in a closer approximation to its body and head, but there's no way to follow the enemy's exact shape.

avatar image
0

Answer by danielreyvega · Jul 02, 2011 at 07:23 PM

Thanks a lot, aldonaletto!!!

i already found that the bullet object was rotated and the raycast poin to the wrong direction.

So this issue is solved. I've modified the rotation and works perfectly! But we still have the problem of the colliders that as you said, will be out of the mesh.

So how could we control the mesh collision?

Thanks man!

Comment
Add comment · Show 3 · 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 aldonaletto · Jul 02, 2011 at 10:47 PM 1
Share

Please read comment in my answer.

avatar image _Petroz · Jul 03, 2011 at 12:45 AM 2
Share

Please don't post comments as answers.

avatar image danielreyvega · Jul 03, 2011 at 10:28 AM 0
Share

I'm sorry i don't read the comments. I reply from my fphone and i canot see very well all the page! 

(9 seconds ago)danielreyvega

avatar image
0

Answer by danielreyvega · Jul 03, 2011 at 10:21 AM

Ok! I'll try some code to solve this, because i've character models with ragdoll and i still looking for i way to use the ragdoll colliders for this issue. But the problem is that these collidrers Affect to the mesh movement in the animation. So i'll have to find another vision to find the way.

For the bullet decals in the character object. If we've to use the coliders it will be more hard that if we find the way to transform the vector 3 coordinates of the impact point to the uv coordinates of the textere and paint a bullet hole directly. But i have a lot of reading and searching work ahead.

Thanks again, and if you have some advice for all this stuff, please tell me.

Cheers!

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 danielreyvega · Jul 03, 2011 at 10:28 AM 0
Share

I'm sorry i don't read the comments. I reply from my fphone and i canot see very well all the page! 

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

problem with instatiated clone 2 Answers

How To: Spawn object on raycast collision. 2 Answers

help with scripting combo points 1 Answer

checking collision with polygon collider using ray cast and instantiating objects issue 1 Answer

How can I continue to instantiate an object after deleting 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