Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to make a simple arrow porjectile

Discussion in '2D' started by Leandro95, Apr 19, 2015.

  1. Leandro95

    Leandro95

    Joined:
    Mar 18, 2015
    Posts:
    16
    Hi,

    I search how to make in unity an arrow trajectory, how to decrease horizontaly the velocity ? For the moment i have a rigth trajectory when i shot a projectile. I just want to make my projectile fall a bit each x secondes .
     
  2. acorrow

    acorrow

    Joined:
    Jan 4, 2015
    Posts:
    32
    Instead of making a trajectory, you could just have it participate in physics and simulate actual gravity on the arrow. However, to reduce the Y position over time....

    I would likely do this in Update rather than FixedUpdate, but I'm not totally sure that it would matter... I think though that if you saw lag in your game at all, you could end up with a jumpy arrow if it were in FixedUpdate... aaaanyway. Someone feel free to correct me here but....

    Code (CSharp):
    1.         float fallSpeed = 0.1f;//The amount you want the arrow to fall every frame (cause we are in UPDATE, this would be every time step if we are in FixedUpdate....)
    2.         while (transform.position.y > 0) //0 us just what I ASSUME is your ground height, this shoudl be the point where the arrow woudl stop...
    3.         {
    4.             //Just set the position to be the same X position as it had, but decrease the Y position by your amount
    5.             transform.position = (new Vector3(transform.position.x, transform.position.y - fallSpeed));
    6.         }
    7.  
    Again, I think you would prob be better to just use physics on this one, and modify the mass/grav scale of the arrow till you find what feels best, but this should work if you just want to slowly move the arrow down... on the Y axis...

    Good luck!
     
  3. MoonJellyGames

    MoonJellyGames

    Joined:
    Oct 2, 2014
    Posts:
    324
    I did something like this when I was working with Flixel in Flash. It would probably be easier to take advantage of Unity's physics engine if you can (I had to do it similar to how acorrow described in Flash). You'll also probably want some rotation as the arrow moves. If you have any questions about this, I can look at my old code to see how I did it exactly.
     
  4. Leandro95

    Leandro95

    Joined:
    Mar 18, 2015
    Posts:
    16
    yes, if u can i will apreciate it
     
  5. MoonJellyGames

    MoonJellyGames

    Joined:
    Oct 2, 2014
    Posts:
    324
    Hey, sorry for the late reply. It's been a very busy week for me.

    So I found my spear-throwing code. It's actually pretty straight-forward. In the spear's update, if the spear is moving left, it also rotates counter-clockwise by one degree; and of course clockwise for if its moving right. My spear had a tiny invisible child object that followed the position of the tip. If the tip hit the ground, the spear would enter its "stuck" state (or whatever I called it) where it stops moving and no longer damages enemies. There's some stuff in my code here that you definitely won't need and the math functions are a little different, but this should get you in the right direction if you haven't already figured it out.

    Since this was from my Flash project, the code is in ActionScript3.

    Code (CSharp):
    1.             //as long as the spear is moving and is not lodged in an enemy or "planted", change the angle
    2.             if (velocity.x != 0 && velocity.y >= 0 && hitEnemy == false && planted == false && stabbing == false)
    3.             {
    4.                 setCenter();
    5.                 tip.velocity = velocity;
    6.                
    7.                 if (velocity.x < 0)        //throwing left
    8.                 {
    9.                     angle -= 1;
    10.                     tip.x = center.x - (radius - 5) * Math.cos(degreesToRadians());
    11.                     tip.y = center.y - (radius - 6) * Math.sin(degreesToRadians());
    12.                 }
    13.                 else if (velocity.x > 0)    //throwing right
    14.                 {
    15.                     angle += 1;
    16.                     tip.x = center.x + (radius - 5) * Math.cos(degreesToRadians());
    17.                     tip.y = center.y + (radius - 6) * Math.sin(degreesToRadians());
    18.                 }
    19.             }