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 /
avatar image
0
Question by huaffinity · Jun 27, 2017 at 04:49 AM · instantiateprefabsdistancelength

Help needed on getting distance value of chain of links after instantiation

 using UnityEngine;
 
 public class Rope : MonoBehaviour
 {
     public GameObject hook;
     public GameObject linkEnd;
     public GameObject linkPrefab;
     public int links;
 
     public float range;
     public float maxRange;
 
     void Start ()
     {
         GenerateRope();
     }
 
     private void Update()
     {
         GetRange();
     }
 
     private void GenerateRope()
     {
         Rigidbody2D previousRB = hook.GetComponent<Rigidbody2D>();
 
         for (int i = 0; i < links; i++)
         {
             GameObject link = Instantiate(linkPrefab, transform);
             HingeJoint2D Hinge = link.GetComponent<HingeJoint2D>();
             DistanceJoint2D Distance = link.GetComponent<DistanceJoint2D>();
             Hinge.connectedBody = previousRB;
             Distance.connectedBody = previousRB;
             Hinge.autoConfigureConnectedAnchor = false;
             Hinge.connectedAnchor = new Vector2(0f, -0.145f);
 
             if (i < links -1)
             {
                 previousRB = link.GetComponent<Rigidbody2D>();
             }
             else
             {
                 linkEnd.GetComponent<LinkEnd>().ConnectLinkEnd(link.GetComponent<Rigidbody2D>());
             }
 
             CalculateLength(links);
         }
     }
 
     private float GetRange()
     {
         return range = Vector2.Distance(hook.transform.position, linkEnd.transform.position);
     }
 
     private void CalculateLength(int links)
     {
         DistanceJoint2D distance = linkPrefab.GetComponent<DistanceJoint2D>();
         HingeJoint2D anchors = linkPrefab.GetComponent<HingeJoint2D>();
         maxRange += distance.distance;
     }
 
     private void OnDrawGizmos()
     {
         Gizmos.color = Color.blue;
         Gizmos.DrawLine(hook.transform.position, linkEnd.transform.position);
     }
 }


Here I have a gameObject that instantiates multiple 'links' (int links) at Start() as children. These chains form a rope and at the end of it, there is a weight connected (GameObject linkEnd). I need two float values in order to make this work with other mechanics; max distance (radius from center to linkEnd's position), and current distance of the same which is tracked real-time. I've managed to get the current distance since it's so simple with VectorX.Distance as shown. However, getting the max distance wasn't. Straight forward max distance of this objects when having 10 and 20 links each gave me number of...

Where x = number of links y = the size / height of each link object and z being the max distance returned by GetRange()

     x * y = z
     20 * y = 87.39162
         y = 4.369581

     10 * y = 46.65462
         y = 4.665462

it just gets me really confused. Even though this difference of y was occurred by the weight of linkEnd (either are identical) it still doesn't give me an answer.

I've tried to get maxRange via adding up the distance of DistanceJoint attached to each links(since I couldn't use HingeJoint to get the right value either), but no lunk. The returned value was also incorrect.

So, simply put: I need maxRange to be calculated at initiation of this script. Can someone give me some good tips please?

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

0 Replies

· Add your reply
  • Sort: 

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

79 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

Related Questions

Need help understanding scripted prefab behavior - when I click one, the script runs on ALL prefab instances, not just the one I clicked. 3 Answers

Animations on instantiated prefabs not working 0 Answers

Instantiate an object in a prefab? 1 Answer

Instantiate only certain objects within a prefab 1 Answer

Invisable Projectile; Instantiate at Collisions 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