# Playmaker Forum

## Playmaker Help & Tips => Playmaker Help => Topic started by: frogwise on November 09, 2017, 02:21:04 PM

Title: Creating a spring-loaded launching mechanic in 3D, ala Angry Birds
Post by: frogwise on November 09, 2017, 02:21:04 PM
Hey guys,

Just trying to think through how I'm going to setup my Angry Birds-like slingshot mechanic, and I could use your help thinking through the FSM logic.

I have a character who's going to cast a fishing line. I want the player to cast the line by clicking on a spot in the water, dragging backwards, and when they let go, the line shoots out a projectile. The projectile flies at a velocity equivalent to the amount that was pulled back.

I'd like to limit the amount of pull-back that can happen. So for example, if a player clicks at the very top of the "lake" that is furthest back, the pullback amount will cap out at a certain distance.

I also need a way to conver the pull back strength into a velocity amount for a physical projectile.

Title: Re: Creating a spring-loaded launching mechanic in 3D, ala Angry Birds
Post by: djaydino on November 09, 2017, 04:53:56 PM
Hi,
I have not tried to build something like that yet,
but there is an asset on the asset store which is build with playmaker called Slingshot Playmaker Kit (https://www.assetstore.unity3d.com/en/#!/content/7761) but it seems to be for mobile.
Title: Re: Creating a spring-loaded launching mechanic in 3D, ala Angry Birds
Post by: tcmeric on November 09, 2017, 06:49:43 PM
In pseudo code, something like this in my mind.

Point A, the sling shot. Click and drag back with the mouse. This is point B.

Measure the distance between A and B. Keep checking it on everyframe. Do a float compare. If it is beyond your max distance, take action to not allow the rope to move farther.

Use distance between point A and B to determine velocity. (Add force). Ie, distance * 2 = force. (You will need to see how much force is best for you).

Use two points to determine heading. Normalize the heading to get direction. Apply force when the mouse button is up, in that direction.
Title: Re: Creating a spring-loaded launching mechanic in 3D, ala Angry Birds
Post by: frogwise on November 10, 2017, 12:10:42 AM

djaydino - I have looked at Slingshot Kit, but it only offers a 2D solution.

tcmeric - How would I go about drawing the expected trajectory curve? It would need it to go out FURTHER from the "slingshot" as you click-drag your mouse CLOSER to the slingshot. I am struggling with the vector math for this.
Title: Re: Creating a spring-loaded launching mechanic in 3D, ala Angry Birds
Post by: tcmeric on November 10, 2017, 09:16:21 AM
I used this asset: https://www.assetstore.unity3d.com/en/#!/content/55752

I wrote a custom action to assist for it: https://github.com/dumbgamedev/general-playmaker/tree/master/projectiles/thirdparty

If you watch the video through, it will also show you how I got direction between game objects etc (custom action).
Title: Re: Creating a spring-loaded launching mechanic in 3D, ala Angry Birds
Post by: frogwise on November 13, 2017, 09:54:44 PM
Brilliant, tcmeric! I followed you video and used your actions. I was able to modify the information to fit my needs, but I still need to do some work regarding the "spring loaded" pull-back mechanic to launch the projectile.

Question - I'd like to give my projectile a drag value of 1, so that it doesn't roll infinitely when it lands. However, when I set the rigidbody drag value to 1 and the Projectile Path Prediction 3D linear drag value to 1, I no long hit my "target" object. I get about halfway. This is obvious, since now the object has some drag and will not make it to the target. How can I get around this?
Title: Re: Creating a spring-loaded launching mechanic in 3D, ala Angry Birds
Post by: frogwise on November 13, 2017, 10:01:38 PM
I solved this, whether correctly or not, by leaving the DRAG at 0 until I collide with the ground, at which point I then set the DRAG to 1.

Thoughts?
Title: Re: Creating a spring-loaded launching mechanic in 3D, ala Angry Birds
Post by: frogwise on December 09, 2017, 10:32:59 PM
Hey tcmeric,

So I have had some success with your methods, but I am finding it difficult to place a reticule marker on the location that my projectile will land.

The reason for this is that I am using a "pull back" mechanic to launch a projectile like a sling shot. So I use the Direction Between Game Objects of the SLINGSHOT and my MOUSE POSITION, and then that direction is used INVERSELY to add a FORCE to the projectile. This means that I am not using a VECTOR POSITION to launch my projectile to, but a VECTOR DIRECTION to apply force to it.

My problem is that I cannot place a visual object at the location the projectile will land (green dot in image) because I do not have that location, just a direction in which the projectile is flying.
Title: Re: Creating a spring-loaded launching mechanic in 3D, ala Angry Birds
Post by: frogwise on December 09, 2017, 10:36:08 PM
Oh, I should mention - the slingshot is able to move around the world (it does not just stay at 0,0,0)