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 Create a Path Projectory From Spring Joint

Discussion in '2D' started by coder_for_life22, Jul 16, 2015.

  1. coder_for_life22

    coder_for_life22

    Joined:
    Dec 25, 2013
    Posts:
    46
  2. shloob

    shloob

    Joined:
    Jun 1, 2013
    Posts:
    10
    I believe that this problem is even more complex than you would think. In that tutorial you are using a spring joint and then saving the final velocity from the point just before the projectile begins to slow down (i.e. just before the spring begins to pull in the opposite direction) and then disconnecting the projectile from the spring and setting the velocity to that max velocity and turning it over to the physics system.. In order to predict the trajectory for the projectile from a given point on the spring, you will need to be able to predict the maximum velocity of the projectile based on the spring constant and the distance from the origin of the spring. Then once you know that value you need to predict the trajectory of the projectile at that velocity and that angle. I am looking to do something similar, and will let you know if I come up with any solutions, but at this point it seems to be anything but trivial. I would start by looking into how the SpringJoint2D works and see if you can get a formula down to predict what the max velocity of the projectile will be from any point away from the spring's origin, and then go from there.
     
  3. Zoltern

    Zoltern

    Joined:
    Jun 16, 2015
    Posts:
    14
    Or try to fake it.

    When the user pulls the string, do throw an invisible game object in that direction, with the same speed and everything, then paing the trail :)
     
  4. shloob

    shloob

    Joined:
    Jun 1, 2013
    Posts:
    10
    The trouble is figuring out what the velocity of the projectile will be when it is released from the spring. Once you can figure that out then that method will work, but as the user moves around you will need to wait for the invisible object to move along the path, and that will be slow. If you look at the tutorial that coder_for_life is following, you will see that once the user lets go of the projectile, they track the velocity of the projectile on the spring, once the current velocity of the projectile is less then the previous velocity they detach the projectile from the spring and set the velocity to the previous velocity. Figuring out what the max velocity of the projectile on the spring will be won't be easy since that will change in relation to how far back the user has stretched the spring.

    If you do wind up knowing your initial velocity, then you can use the method they discuss here for a more real-time update (See the method suggested by gfoot).

    http://forum.unity3d.com/threads/projectile-prediction-line.143636/
     
    Last edited: Jul 23, 2015
    theANMATOR2b likes this.
  5. Zoltern

    Zoltern

    Joined:
    Jun 16, 2015
    Posts:
    14
    Indeed. My mistake. I thought i read somewhere that you could change the timescale for a group of objects, but upon further research i found that you can't thou :-(
     
  6. shloob

    shloob

    Joined:
    Jun 1, 2013
    Posts:
    10
    Haha, no worries, I updated my previous post with a link to an approach that seems like it will work faster, but still doesn't solve the problem of not knowing initial velocity.
     
  7. Zoltern

    Zoltern

    Joined:
    Jun 16, 2015
    Posts:
    14
    Actually, it seems you can alter the timescale. But it would be bad anyway, as timescale only affects FixedUpdate and not Update. So, yeah, gonna have to caculate it