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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

2d projectile curve

Discussion in '2D' started by justindfunk, Jul 23, 2018.

  1. justindfunk

    justindfunk

    Joined:
    Jul 21, 2016
    Posts:
    7
    I'm having trouble getting projectiles to move how I want. I started with firing a projectile in a straight line towards a target at a certain velocity. The method is to rotate the projectile towards the target and then apply the desired velocity to the rigidbody along the forward vector, in my case Up since this is a vertical scroller. Here's the code:

    Code (CSharp):
    1. Vector3 dir = target.transform.position - transform.position;
    2. var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Def;
    3. transform.rotation = Quaternion.AngleAxis(angle - 90.0f, Vector3.forward);
    4.  
    5. RigidBody2d rb2d = GetComponent<Rigidbody2d>();
    6. rb2d.velocity = transform.up * mps;
    This part works fine. The problem I'm having now is that I want to fire multiple projectiles at once, have them fire out across a 45 degree arc, and then curve in towards the target. My thinking was I accomplish simply by setting the original rotation to whatever the new firing angle is, which is the original angle +/- some value between -45/45, and then in on update recalculate and reapply the rotation and set the velocity using the new vector.

    The update portion is basically the same, except it uses a Slerp between the current rotation and new angle:

    Code (CSharp):
    1. transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.AngleAxis(angle - 90.0f, Vector3.forward), 5f * Time.deltaTime);
    However it doesn't work. I haven't even gotten to the point of testing it with multiple projectiles along the arc. Even the original setup of firing one projectile directly at the target doesn't work, with the projectiles moving at really slowly, and get slower the closer they get to the target.
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    The final parameter of Slerp is a value between 0 and 1. Zero being the starting rotation, One being the final rotation. Since you've got
    transform.rotation
    as your start point, which is constantly updating, you're rotating
    5f * Time.deltaTime
    percent towards the final rotation every frame, which is a smaller and smaller value every frame as
    transform.rotation
    approaches the final rotation.

    You might want to use Quaternion.RotateTowards.
     
  3. justindfunk

    justindfunk

    Joined:
    Jul 21, 2016
    Posts:
    7
    Okay, I changed it to

    Code (CSharp):
    1. transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.AngleAxis(angle - 90.0f, Vector3.forward), 45.0f * Time.deltaTime);
    but it's still not working right. The projectile slows down at intervals. I tweaked some stuff so that it's firing two projectiles straight towards the mouse click at once, one using the rotation function and one not so that I could compare, and the one using the function lags behind. What's interesting is that the gap between the two depends on where I fire. If I fire straight up the gap is minimal, the rotation projectile only slowing down once right before it reaches the target point. The farther away from up I fire, the more slow down intervals it goes through.

    I did some console debugging to monitor the magnitude of the velocity vector and it stays constant always. I also output the rotation value and it stays relatively constant, with the only change in the value occurring in the fifth+ decimal place.

    Also worth noting that the actual rotation part of it works fine. If I do the split projectile at +/- 45 degree angles they correctly shoot out and then curve back in towards the target. I also added a line renderer to show the forward vector so I could check for wobble but it looks good and is always pointing the expected direction in both single and split modes. The only issue is the speed isn't staying constant.
     
  4. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,834
    You might try "alpha blending" between a) the position on a curve which represents the starting launch trajectory/path of the missile, and the position of the enemy. You can then 'cross fade' the average between the two positions in order to transition from the launch path and the enemy location. I don't really see why you'd want to do a physics simulation for this unless you want it to be influenced by gravity and natural forces to find its way to a destination. Making home in on a specific target is going to require influencing the forces outside of the physics system.
     
  5. Bodna_Frescu

    Bodna_Frescu

    Joined:
    Mar 7, 2021
    Posts:
    1
    What is rad2def?
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,470