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!
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;
}
}
@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.