[SOLVED] Instantiating in wrong direction after colliders touch

Updated on March 29, 2016 in [A] Unity Scripting
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
10 on March 26, 2016

Hi everyone, i have the following issue: my player has a circle collider 2D and so do my enemies. The player shoots by instantiating bullets at the centre of his gun which is located slightly in front of him. At first, shooting works perfectly, but if the enemies hit me then something goes wrong and my bullets travel in slightly different directions.. strange thing is, it never did this before. Im not sure if a unity update could change how things behave in my scene (?). Pls let me know if you have any ideas, thx in advance!

  • Liked by
Reply
0 on March 26, 2016

It may be something with the colliders on you gun/character that is messing with the physics. Can you post screenshot of your error?

  • Liked by
Reply
Cancel
0 on March 26, 2016

Okay, as soon as i can i will, thx!

  • Liked by
Reply
Cancel
2 on March 28, 2016

So, I have taken the screenshot showing the bullets going in different directions, but I cannot upload it since the admin disabled the “upload picture” function..

Helpful
on March 28, 2016

Good suggestion 🙂

Show more replies
  • Liked by
Reply
Cancel
0 on March 29, 2016

So, here is the image. As can be seen, the first (further to the left) bullet is travelling straight where the player is pointing, while the two newer bullets (to the right) are travelling in another direction even if the player/gun are still pointing in the previous direction. This happened after impact between the player and enemies. And everytime an enemy hits the player, the pattern with which bullets travel keeps slightly changing and sometimes it even goes back to normal.

  • Liked by
Reply
Cancel
3 on March 29, 2016

Check if the player Transform is rotating after the impact. I’ve had some similiar behaviour when using Rigidbodies… have you set up the constraints? and what gameobject/position is the origin of your bullets?

Helpful
on March 29, 2016

Whoa! Yes it works! I have to look into that, I froze the rotation about Z and now it’s behaving properly! Also, after collisions I was getting another problem: the player was slightly rotating by himself (very slightly) which was annoying but I just thought maybe it was an error in my script, but after adding that constraint it stopped. The origin of the bullets is at the centre of my weapon, which is located very close to the player’s collider. But now it seems to work perfectly after the introduction of that constraint, thanks man!! I will go look into those constraints to understand them properly, but could you tell me how you knew it could have solved the bullet problem? Thx again!

Master
on March 29, 2016

Since the player has a rigidbody it can be affected by objects colliding with it. But the constraints give the option to still move stuff around using physics but without the rotation that should be applied. But mostly I knew from having had the same problem 😛

Helpful
on March 29, 2016

Good to know! Thank you!

Show more replies
  • Liked by
Reply
Cancel