Search Unity

I shot an arrow up in the air...

Discussion in 'Scripting' started by quickrice, Apr 29, 2009.

  1. quickrice

    quickrice

    Joined:
    Mar 28, 2009
    Posts:
    22
    I am shooting a projectile from a weapon. Call it an arrow, missile, badminton shuttlecock, whatever. The initial trajectory of the projectile matches the weapon at launch time. All good so far.

    I can't, however, get the projectile to rotate so that it matches the trajectory path (like an actual arrow). So my arrows always land on their tail when they hit the ground. The trajectory could be relatively flat OR it could be a shot that is nearly straight up. Does anyone know how to do this?

    This is my first Unity project and I hope I'm missing something obvious. I have spent 2 solid days trying to get this simple behavior to work. I have wrapped myself around the axle with quaternions, and Euler angles and inertia tensors and, angular velocities, and ... until I'm completely lost. I was really hoping Unity would shield me from having to have a degree in physics but so far the documentation and 'samples' provided are falling pretty short of my expectations considering the cost of this software.

    I'm still holding out hope that my frustration is just due to my lack of experience and ignorance of Unity's capabilities. Somebody just tell me where the checkbox is that tells a rigidbody to rotate along it's trajectory path and I'll take it all back.

    Thanks for the help and for tolerating the rant.
     
  2. CoatlGames

    CoatlGames

    Joined:
    Apr 25, 2008
    Posts:
    773
    have you set proportions, mass and center of mass of your projectile correctly??

    does your projectile has a heavier portion in the head??? so that it flies and falls along the trajectory like it should ???

    it sounds like either all your projectile has the same weight or there is heavier portions in the tail to make the rigid body fall always on their tails

    use rigidbody.SetDensity(1.0);

    in your rigid bodies to calculate the appropriate mass, just change the density value according to the material hardness ,also center of mass is a very very important setting as it handles the center of gravity of the object and pretty much affects every motion on it including rotations

    example:

    rigidbody.centerOfMass = Vector3.zero;

    also make sure your using the right amount of force or velocity in the right direction, for a projectile that flies and falls it should be applying force or velocity in local space(relative force) in the front axis of the object,it may be either x or z depending on the models orientation so that no matter if the model rotates in world space, its always pushing the rigid body forward in local space so it would fly just fine, just make sure there's something heavy in the head of the projectile

    hope this helps 8)
     
  3. pete

    pete

    Joined:
    Jul 21, 2005
    Posts:
    1,647
    yeah probably the easiest:

    rigidbody.centerOfMass = Vector3.(0,0,0.1);

    the 0.1 is in the z which puts it 10cm forward. Vector3.zero is 0,0,0. declare a public var for the z and you can adjust as needed in the inspector.

    you can also put an empty with a rigidbody slightly forward of center and parent it to your arrow. adjust it's mass etc.

    you could just calculate the trajectory and translate accordingly which can give you a performance benefit over the PhysX engine if you have a million projectiles. er thousands... er :D
     
  4. quickrice

    quickrice

    Joined:
    Mar 28, 2009
    Posts:
    22
    Thank you so much for the responses. Whatever difficulties I may voice about Unity certainly don't apply to the great community of people using the product. Thank you again for your willingness to help.

    However... I am now at the end of day 3 of working on nothing but getting my arrow to rotate along the trajectory path and am no closer to getting it to work. There is one significant difference in my environment versus the proposed solutions to the problem: My weapon and projectiles were designed vertically not horizontally. This is because most of my shots and the orientation of the weapon are more "up" than "out". The projectile is instantiated with the same position and rotation as the weapon, but the (relative) force is applied to the Y axis to get the projectile to fire, not the Z axis.

    Could this be why none of the suggestions work? The trajectory (not the rotation) of the projectile looks and acts correct. But moving the center of mass to the top (0,1,0), adding a weighted body to the head of the projectile, and other attempts don't change anything in how the projectile rotates.

    I have quite a lot of other scripts that manipulate different features of the weapon, so changing it's axis orientation would be extremely difficult.

    Thanks,
    Steve
     
  5. WillBellJr

    WillBellJr

    Joined:
    Apr 10, 2009
    Posts:
    394
    This is a great post being a newbie myself - the physics in Unity are very good - and as you've felt, it's most likely something simple that you've overlooked.

    I've been using rigidbodies for a while now and never really set the proper center of mass for my objects - so I've learned something here myself! :p


    I believe what you last stated may be your prob - I believe your arrow should be modeled facing down the Z axis and should be traveling along its Z direction instead of Y when moving.

    I've seen a lot of 3D animation setups (other than Unity) that depend on a "Z-forward" paradigm, this may be your problem here as well.

    Looking forward to you finding the solution to this problem! :wink:

    -Will

    PS - Thinking about it a bit though, I don't know if it should really matter what axis you're using whether Y or Z??

    What goes up must come down provided there's gravity - perhaps you need to have variable drag across the body of the arrow?
     
  6. Rapzid

    Rapzid

    Joined:
    Apr 25, 2009
    Posts:
    123
    I would create a 3 part arrow of some sort using fixed joints. The center being the shaft, the tip having slightly more mass and the rear, being the feathers, more DRAG. The drag from the feathers should keep the arrow point toward where the tip is pulling the arrow.
     
  7. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    Bah. It's easier just to use this code:

    Code (csharp):
    1. function Update()
    2. {
    3.    transform.LookAt(transform.position + rigidbody.velocity);
    4. }
    It'll only work if your arrow's tip is towards positive Z, though. If it isn't, you'll have to rotate it by 90 or 180 degrees after the look.
     
    TomWong likes this.
  8. quickrice

    quickrice

    Joined:
    Mar 28, 2009
    Posts:
    22
    Thanks to all who contributed to this thread. I now have arrows that fly in a beautiful arc and behave just like real arrows!

    - GargerathSunman - Your one line of code looks like a wonderfully elegant solution to the problem. Due to my inexperience, I would have never thought you could manipulate velocity vectors and transforms like that. If I were doing this over, I would start with that. As it happens, however, I got this working just before you made your post and I AIN'T GONNA CHANGE IT to see if your way would work!

    - The (decidedly in-elegant) solution involved creating an arrow with 3 parts as Rapzid suggested. Adding a weighted rigidbody to the tip of the arrow by itself didn't seem to do anything. In conjunction with another rigidbody at the tail of the arrow with a high Drag value, however, made the difference.

    So, it didn't seem to matter that my 'firing force' was being applied to the Y rather than the Z axis. Adjusting the values on the 2 extra rigidbodies along with the arrow rigidbody itself is a bit like balancing marbles, but it does work. Fortunately, I will only ever have one arrow at a time in play, so the extra resources this solution requires isn't a concern.

    Thanks again to all who contributed. I really appreciate it.
    Steve
     
  9. wccrawford

    wccrawford

    Joined:
    Sep 30, 2011
    Posts:
    2,039
    You just resurrected a post that was 4 years old.
     
  10. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    I only see your post... are you talking to yourself? ;)
     
  11. Velo222

    Velo222

    Joined:
    Apr 29, 2012
    Posts:
    1,437
    Wow good necro lol.

    The title sounds like the beginning of an epic poem.