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 palinalif123 · Mar 04, 2019 at 02:45 PM · scripting problemtransformobjectlinerendererrope

Change Script so that the object's position can be modified in the Editor/through other scripts?

Hi! I have a script that I got from elsewhere that I want to modify so that I can freely move the object at the end of the line around, but I'm unsure as to how to do that since I didn't write the code myself. Any suggestions would be very welcome!

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 //Simulate a rope with verlet integration and no springs
 public class RopeControllerRealisticNoSpring : MonoBehaviour
 {
     //Objects that will interact with the rope
     public Transform whatTheRopeIsConnectedTo;
     public Transform whatIsHangingFromTheRope;
 
     public int maxSections;
     public float ropeSectionLength; //progressBarCalculation changes this
 
     //Line renderer used to display the rope
     private LineRenderer lineRenderer;
 
     //A list with all rope section
     private List<RopeSection> allRopeSections = new List<RopeSection>();
 
     private void Start()
     {
         //Init the line renderer we use to display the rope
         lineRenderer = GetComponent<LineRenderer>();
 
         //Create the rope
         Vector3 ropeSectionPos = whatTheRopeIsConnectedTo.position;
         for (int i = 0; i < maxSections; i++)
         {
             allRopeSections.Add(new RopeSection(ropeSectionPos));
 
             ropeSectionPos.y -= ropeSectionLength;
         }
     }
 
     private void Update()
     {
         
         //Display the rope with the line renderer
         DisplayRope();
 
         //Move what is hanging from the rope to the end of the rope
         whatIsHangingFromTheRope.position = allRopeSections[allRopeSections.Count - 1].pos;
 
         //Make what's hanging from the rope look at the next to last rope position to make it rotate with the rope
         whatIsHangingFromTheRope.LookAt(allRopeSections[allRopeSections.Count - 2].pos);
     }
 
     private void FixedUpdate()
     {
         UpdateRopeSimulation();
     }
 
     private void UpdateRopeSimulation()
     {
         Vector3 gravityVec = new Vector3(0f, -9.81f, 0f);
 
         float t = Time.fixedDeltaTime;
 
 
         //Move the first section to what the rope is hanging from
         RopeSection firstRopeSection = allRopeSections[0];
 
         firstRopeSection.pos = whatTheRopeIsConnectedTo.position;
 
         allRopeSections[0] = firstRopeSection;
 
 
         //Move the other rope sections with Verlet integration
         for (int i = 1; i < allRopeSections.Count; i++)
         {
             RopeSection currentRopeSection = allRopeSections[i];
 
             //Calculate velocity this update
             Vector3 vel = currentRopeSection.pos - currentRopeSection.oldPos;
 
             //Update the old position with the current position
             currentRopeSection.oldPos = currentRopeSection.pos;
 
             //Find the new position
             currentRopeSection.pos += vel;
 
             //Add gravity
             currentRopeSection.pos += gravityVec * t;
 
             //Add it back to the array
             allRopeSections[i] = currentRopeSection;
         }
 
 
         //Make sure the rope sections have the correct lengths
         for (int i = 0; i < 20; i++)
         {
             ImplementMaximumStretch();
         }
     }
 
     //Make sure the rope sections have the correct lengths
     private void ImplementMaximumStretch()
     {
         for (int i = 0; i < allRopeSections.Count - 1; i++)
         {
             RopeSection topSection = allRopeSections[i];
 
             RopeSection bottomSection = allRopeSections[i + 1];
 
             //The distance between the sections
             float dist = (topSection.pos - bottomSection.pos).magnitude;
 
             //What's the stretch/compression
             float distError = Mathf.Abs(dist - ropeSectionLength);
 
             Vector3 changeDir = Vector3.zero;
 
             //Compress this sections
             if (dist > ropeSectionLength)
             {
                 changeDir = (topSection.pos - bottomSection.pos).normalized;
             }
             //Extend this section
             else if (dist < ropeSectionLength)
             {
                 changeDir = (bottomSection.pos - topSection.pos).normalized;
             }
             //Do nothing
             else
             {
                 continue;
             }
 
 
             Vector3 change = changeDir * distError;
 
             if (i != 0)
             {
                 bottomSection.pos += change * 0.5f;
 
                 allRopeSections[i + 1] = bottomSection;
 
                 topSection.pos -= change * 0.5f;
 
                 allRopeSections[i] = topSection;
             }
             //Because the rope is connected to something
             else
             {
                 bottomSection.pos += change;
 
                 allRopeSections[i + 1] = bottomSection;
             }
         }
     }
 
     //Display the rope with a line renderer
     private void DisplayRope()
     {
         float ropeWidth = 0.03f;
 
         lineRenderer.startWidth = ropeWidth;
         lineRenderer.endWidth = ropeWidth;
 
         //An array with all rope section positions
         Vector3[] positions = new Vector3[allRopeSections.Count];
 
         for (int i = 0; i < allRopeSections.Count; i++)
         {
             positions[i] = allRopeSections[i].pos;
         }
 
         lineRenderer.positionCount = positions.Length;
         lineRenderer.SetPositions(positions);
     }
 
     //A struct that will hold information about each rope section
     public struct RopeSection
     {
         public Vector3 pos;
         public Vector3 oldPos;
 
         //To write RopeSection.zero
         public static readonly RopeSection zero = new RopeSection(Vector3.zero);
 
         public RopeSection(Vector3 pos)
         {
             this.pos = pos;
 
             this.oldPos = pos;
         }
     }
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by kaplica · Mar 04, 2019 at 02:52 PM

Scripts are in Unity components, which reside on game objects. Every script in unity that resides on a game object needs to inherit from MonoBehavior, thanks to which you can do GetComponent to get a script off a certain game object.

 var go = GameObject.Find("name of gameobject");
 var script = go.GetComponent<yourType>();

if the other script resides on the same gameobject you just get the script.

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

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

194 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 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 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

How to diappear a line renderer from start and end position of line 1 Answer

Line render from an object to a click 0 Answers

Help with Rotating code? 2 Answers

Move gameObject with mouse 1 Answer

how to find a point(vector2) between two points(Vector2) of a line (line renderer) 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