I’ve made a simple script which shots an arrow wherever the character is pointing at. This arrow is actually a rigidbody, with a given initial speed, but i’ve found several problems in my code which I don’t seem to be able to solve.
First of all, the collision detection. If the arrows are trhown with a speed value larger or around 50 (50*Time.deltaTime, so I would call it, meters per second), it ignores some collisions with colliders. This seems to be a problem of the arrow being behind the collider in one frame, and after the collider at the next frame, so it hasn’t touched that collider actually. I don’t know if there’s a way to fix this, or it’s just that I have to code some previous calculations with Raycast, before shooting the arrow, or something like that.
Next, I don’t seem to be able to find a way for my arrow to rotate to always make it’s arrowhead always point down. I’ve tried to set the center of mass to the arrowhead, but that only resulted on a strange behaviour rather than rotating the arrow.
Thank you very much
Your first problem might be solved using a for loop, but I think it is not efficient.
My idea is to have a a for loop which tests your collisions.
You can write the code inside a function and call the function in start function.
It might look something like this:
private var collision:GameObject;
this.collision = collision;
TestCollision(collision); // send as a parameter the object you want to test with
// test for your collision
And for your second problem, you can use two different rigidbodies where the head of the arrow is much more massive, but you have to attach them, or try to make the tail child to the head(didn’t try).
Well, I’ll post what ended up working for me, in the case it’s helpful to anybody.
First of all, to my first problem, i’ve made a code which basically does what @asafsitner said commenting my first question. Using a raycast between the arrow and the arrow’s position in the last frame and checked if it missed some collision. That worked just fine, but I also noticed that the problem didn’t fix if i shot arrows to a unity’s default plane (I replaced the plane’s mesh collider to a box collider and it worked)
To my second question, I did what @Vicenti said in my comment, basically, rotate each frame the arrow’s rotation to always look in the direction of the speed vector. But I added a Slerp function to that, so it doesn’t rotate that roughly, just in case.
So that solved it for me, As they weren’t many answers in the “answers” part, and most of them were comments, i’ve tried to summarize what worked for me as an answer.
Old question but since I stumbled across it I thought I should mention what worked for me. You need to adjust the collision detection settings for the rigidbody from discrete to continuous or continuous dynamic if the projectile will be moving quickly and interacting with mesh colliders. This is outlined in the Rigidbody section of the reference manual here http://docs.unity3d.com/Documentation/Components/class-Rigidbody.html