Projectile in angry birds not working as expected
I've been learning unity and following along with the tutorial here
I've completed part one of the tutorial and can't seem to figure out why my projectiles don't shoot. I've compared my script to the one found in the example game of the asset store, and I don't see a difference (except for the fact I used sling instead of catapult -- this is reflected in my scene too).
Here's a gif of what's happening -- it's like the projectile doesn't move forward at all.. http://g.recordit.co/sd03w47xjd.gif
Here's my current script -- did I miss something in the unity scene editor maybe? Many thanks in advance for any help / suggestions!
public class ProjectileDragging : MonoBehaviour {
public float maxStretch = 3.0f;
public LineRenderer slingLineFront;
public LineRenderer slingLineBack;
private SpringJoint2D spring;
private Ray rayToMouse;
private Ray leftSlingToProjectile;
private float maxStretchSqr; // max stretch to power of 2
private Rigidbody2D rb;
private CircleCollider2D circle;
private float circleRadius;
private Transform sling;
private bool clickedOn;
private Vector2 prevVelocity;
void Awake()
{
rb = GetComponent<Rigidbody2D>();
spring = GetComponent<SpringJoint2D>();
circle = GetComponent<CircleCollider2D>();
sling = spring.connectedBody.transform;
}
// Use this for initialization
void Start()
{
LineRendererSetup();
rayToMouse = new Ray(sling.position, Vector3.zero);
leftSlingToProjectile = new Ray(slingLineFront.transform.position, Vector3.zero);
maxStretchSqr = maxStretch * maxStretch;
circleRadius = circle.radius;
}
// Update is called once per frame
void Update()
{
// when clicking on the projectile, enable the drag behaviors
if (clickedOn) {
Dragging();
}
// if we have a spring attached
if (spring != null) {
// if we've released the projectile, but its velocity is starting to slow down
if (! rb.isKinematic && prevVelocity.sqrMagnitude > rb.velocity.sqrMagnitude) {
Destroy(spring);
rb.velocity = prevVelocity;
}
// Set our previous velocity variable for the next frame;
if (!clickedOn) {
prevVelocity = rb.velocity;
}
LineRendererUpdate();
} else {
slingLineFront.enabled = false;
slingLineBack.enabled = false;
}
}
/**
* Setup the line renderers for the sling's rubber bands
*/
void LineRendererSetup()
{
// Set starting position
slingLineFront.SetPosition(0, slingLineFront.transform.position);
slingLineBack.SetPosition(0, slingLineBack.transform.position);
// Set sorting layer as 'foreground'
slingLineFront.sortingLayerName = "Foreground";
slingLineBack.sortingLayerName = "Foreground";
// Set the order within the layer
slingLineFront.sortingOrder = 3;
slingLineBack.sortingOrder = 1;
}
void OnMouseDown()
{
spring.enabled = false;
clickedOn = true;
}
void OnMouseUp()
{
spring.enabled = true;
rb.isKinematic = false;
clickedOn = false;
}
// Dragging the asteroid
void Dragging()
{
// get the point in game space based on mouse position
Vector3 mouseWP = Camera.main.ScreenToWorldPoint(Input.mousePosition);
// get sling position compared to the mouse
Vector2 slingToMouse = mouseWP - sling.position;
// check if we're max distance on the pull, if it is
// then we set the ray direction to be toward the mouse and
// the asteroid on a point at the max stretch value within that ray
if (slingToMouse.sqrMagnitude > maxStretchSqr) {
rayToMouse.direction = slingToMouse;
mouseWP = rayToMouse.GetPoint(maxStretch);
}
// set z to zero, this is 2D
mouseWP.z = 0f;
// set the transform of this object to be the mouse's position
transform.position = mouseWP;
}
void LineRendererUpdate()
{
Vector2 slingToProjectile = transform.position - slingLineFront.transform.position;
leftSlingToProjectile.direction = slingToProjectile;
Vector3 holdPoint = leftSlingToProjectile.GetPoint(slingToProjectile.magnitude + circleRadius);
slingLineFront.SetPosition(1, holdPoint);
slingLineBack.SetPosition(1, holdPoint);
}
}
Answer by 406digital · Sep 17, 2016 at 09:25 PM
I figured it out -- had to uncheck 'Auto Configure Distance' on the spring joint
Answer by uec1 · Apr 28, 2017 at 05:59 AM
The AutoConfigureDistance pointer sorted this for me too - thanks for posting that.
I have another behavior issue though.
When releasing the projectile, it seems to be pulled towards the ground increasingly strongly during the elastic band contraction (i.e. pre-catapult-release) stage, depending on how far 'up' you go from an angle of about 250 degrees. This holds true til about maybe 150 degrees. At this point it starts behaving more as expected - i.e. accelerating along the approximate angle of the ray / line renderer.
In the video, Adam's version does not do this, tracking nicely along the direction of the line renderer until clear of the catapult, pretty much regardless of angle - particularly around the horizontal.
I've tried tweaking the mass of the asteroid, spring 'strength', gravityScale on the Asteroid's RigidBody2D etc, but nothing changes the behavior. With the gravity scale tweak, I set that to 0.01 in onMouseUp() then set it back to 1 at the release point code (i.e. when we destroy the spring).
Does anyone else get that behavior? Essentially, the asteroid hold-release-point-to-catapult direction of travel describing a concave arc with a more pronounced effect the closer to horizontal? If so, has anyone found a fix?
I've not posted my code as it's essentially the same as 406digital's above, but I can do if it would help.
Thanks!
Answer by mrmeizongo · Dec 26, 2018 at 06:32 PM
I did everything I even tried tweaking the code a little bit, watched the whole video again to see what I missed and did what fixed it for you but for some reason it just doesn't work for me. I even copied and pasted your code and reassigned the variables and I'm still not getting the desired behavior.
Disregard, I found the problem. I had unchecked the 'Simulate' box on the rigidbody attached to the catapult. Works perfectly now and I don't encounter the same problem uec1 has.
Your answer
Follow this Question
Related Questions
Why is my momentum not carrying over to the X axis? 0 Answers
Manually created gravity too powerful 0 Answers
Hi! I am trying to use SceneManagement, but I keep getting errors. 1 Answer
How to Set Gravity for large sprites ? 0 Answers
Counteract gravity 2 Answers