Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by 406digital · Sep 17, 2016 at 05:15 PM · scripting problemscenegravityphysics2d

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);
     }
 }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by 406digital · Sep 17, 2016 at 09:25 PM

I figured it out -- had to uncheck 'Auto Configure Distance' on the spring joint

alt text


screen-shot-2016-09-17-at-32416-pm.png (56.4 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mrmeizongo · Dec 26, 2018 at 06:57 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

89 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges