• 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 r-dickinson · Aug 01, 2020 at 10:16 AM · rigidbodyscaling

Scaling and moving two rigidbodies

I have a crane like structure that is comprised of 3 rigidbodies connected with a Hinge Joint and Fixed Joint. I would like to scale the crane cable so that green cylinder rigidbody at the end of the cable lowers. The 'cable' is also cylinder rigidbody. Whatever I try I can't seem to scale the cable in such a way that it moves the green cylinder at the bottom of the 'cable' up and down.

I'm aware of rope solutions but these are a bit overkill for what I need. Hoping someone can point out something easy that I have overlooked! alt text

crane.png (120.3 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by r-dickinson · Aug 02, 2020 at 09:31 AM

@Kaart Thanks very much! I wasn't expecting a complete script! I've used MoveTowards on the other elements so very familiar with that. Will give this a try tomorrow.

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 Kaart · Aug 01, 2020 at 01:22 PM

You need to move the cable and green cylinder at the same time as scaling the cable. Scaling the cable causes it to grow or shrink on both sides so you have to move the cable up or down accordingly.

Here is an example script that uses Vector3.Lerp to interpolate the positions and scale of the cable and green cylinder when scaling the cable, I hope this helps.

 using UnityEngine;
 
 public class CraneExample : MonoBehaviour
 {
     // Set the green cylinder in the inspector (editor).
     [SerializeField]
     private GameObject _greenCylinder;
 
     private Vector3 _greenCylinderStartPosition;
     private Vector3 _greenCylinderEndPosition;
 
     private Vector3 _cableStartPosition;
     private Vector3 _cableEndPosition;
 
     private Vector3 _cableStartScale;
     private Vector3 _cableEndScale;
 
     private float _greenCylinderJourney;
     private float _cableMovingJourney;
     private float _cableExtensionJourney;
 
     private float _startTime;
 
     // This is how fast the cable can extend
     private float _extendSpeed = 1f;
 
     private bool _isExtendingCable;
 
     private void Start()
     {
         ExtendCable();
     }
 
     private void Update()
     {
         if (_isExtendingCable)
         {
             float extensionCovered = (Time.time - _startTime) * _extendSpeed;
 
             float greenCylinderFractionCompleted = extensionCovered / _greenCylinderJourney;
             float extensionFractionCompleted = extensionCovered / _cableExtensionJourney;
             float movingFractionCompleted = extensionCovered / _cableMovingJourney;
 
             _greenCylinder.transform.position = Vector3.Lerp(_greenCylinderStartPosition, _greenCylinderEndPosition, greenCylinderFractionCompleted);
             transform.localScale = Vector3.Lerp(_cableStartScale, _cableEndScale, extensionFractionCompleted);
             transform.localPosition = Vector3.Lerp(_cableStartPosition, _cableEndPosition, movingFractionCompleted);            
 
             if (extensionFractionCompleted >= 1f)
             {
                 _greenCylinder.transform.localPosition = _greenCylinderEndPosition;
                 transform.localScale = _cableEndScale;
                 transform.localPosition = _cableEndPosition;                
                 _isExtendingCable = false;
             }
         }
     }
     private void ExtendCable()
     {
         _greenCylinderStartPosition = _greenCylinder.transform.localPosition;
         _cableStartPosition = transform.localPosition;
         _cableStartScale = transform.localScale;
 
         // Just an example, use the correct Y values here.
         _greenCylinderEndPosition = new Vector3(
             _greenCylinderStartPosition.x,
             _greenCylinderStartPosition.y - 1f,
             _greenCylinderStartPosition.z);
 
         _cableEndPosition = new Vector3(
             _cableStartPosition.x,
             _cableStartPosition.y - 1f,
             _cableStartPosition.z);
 
         _cableEndScale = new Vector3(
             _cableStartScale.x, 
             _cableStartScale.y + 1f, 
             _cableStartScale.z);
 
         _greenCylinderJourney = Vector3.Distance(_greenCylinderStartPosition, _greenCylinderEndPosition);
         _cableExtensionJourney = Vector3.Distance(_cableStartScale, _cableEndScale);
         _cableMovingJourney = Vector3.Distance(_cableStartPosition, _cableEndPosition);        
 
         _startTime = Time.time;
         _isExtendingCable = true;
     }
 }
 
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 Kaart · Aug 01, 2020 at 08:19 PM 0
Share

I just realised you could probaly use Vector3.$$anonymous$$oveTowards() to move the green cylinder and cable instead of Vector3.Lerp(). See https://docs.unity3d.com/ScriptReference/Vector3.$$anonymous$$oveTowards.html for more information.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

182 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

Related Questions

Collider Doesn't Rotate in Sync With Mesh 1 Answer

What should my gravity be if I've modeled in feet and inches in Max? 3 Answers

Rigibody2D.AddForce within a scaled GameObject 0 Answers

scaling objects with rigidbodies 2 Answers

Rigidbody walker scaling problem 0 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