How do I create Sling-Shot like propulsion?
This is my first post, so please excuse any errors in my level of detail or wording. But ultimately, I am trying to accomplish this:
My character (who is on a linear path with a constant velocity, not user controlled) gets tossed into the air and will attach itself to a spinning wheel (think like a giant clock tower gear via a touch and hold control). I'm able to get this far by coding with colliders and parenting the wheel to my character...but what I have trouble with is letting go and having the character "sling-shot" off the wheel naturally, so that you take off in the direction and speed the wheel was rotating where you let go. The best example to reference would be the graspable wheels in Little Big Planet.
I have tried to research this for hours but to no avail. Any help or guidance would be greatly appreciated, thanks!
I would try to use the current and previous position of the character to calculate a speed and direction vector.
@doublemax That definitely sounds like a great start. I'm still pretty new to coding though and may need some additional help if you don't $$anonymous$$d. I know how to calculate Distance usingfloat distance = new Vector2.Distance (currLoc, prevLoc);
therefor creating a float to use as my speed reference...but I'm stumped on how to store the previous position of my player from the last frame, and then to calculate the direction of it along both X and Y axes so that I can "shoot" the character. I'm assu$$anonymous$$g I'll use AddForce with Force$$anonymous$$ode2D.Impulse?
I think, whenever you calculate your vector (you don't need just the float but a direction too, so keep the vector as a whole for the force), maybe in LateUpdate, save the currentposition as the last one. This way, when you encounter the calculation of the actual slingshot, you have both positions. Of course it will fail during the first frame, but who is able to attach and detach in one go ;)
Answer by roblind3 · Oct 22, 2016 at 12:31 AM
Thanks @hexagonius. I was able to finally think up and play around with the solution! My code is pasted below, I had to manually multiply the distance and speed to seem more natural. Let me know if anyone thinks this could be simplified or made smoother. Thanks again! **On a side note, the SlingShot() function is called from another script that controls the touch gestures.
public class SlingShotScript : MonoBehaviour {
private Rigidbody2D body;
private Vector2 prevLoc;
private Vector2 currLoc;
private Vector2 distance;
private float speed;
void Awake()
{
prevLoc = transform.position;
}
void Start()
{
body = GetComponent<Rigidbody2D>();
}
public void SlingShot()
{
distance = (currLoc - prevLoc) * 10f;
speed = distance.magnitude * 2f;
body.AddForce(distance * speed, ForceMode2D.Impulse);
}
void Update()
{
prevLoc = transform.position;
}
void LateUpdate()
{
currLoc = transform.position;
}
}
Ok I take it back...it does work perfectly as far as direction goes...but speed is iffy and inconsistent. Only thing I can guess is due to the way it Updates?? Sometimes it takes off, and other times it barely moves. Any thoughts?