Quantcast
Viewing latest article 30
Browse Latest Browse All 168

How to make object follow another object by scaling his size,how to make an object follow another object by scaling?

I want to scale an object in Unity based on the position of another object, in other word I want the cube to keep following the the second one by scaling in z scale ,were the first cube is attached to the second one, I have a code, when I run it, the first cube scale directly to the center of the second one, than when the second cube start moving the first cube increase in the scale in weak movement, and it left space between him and the second cube. script: using UnityEngine; public class CubeScaler : MonoBehaviour { public Transform object; public float maxScale = 2f; public float scaleFactor = 0.1f; private Vector3 initialScale; private void Start() { initialScale = transform.localScale; } private void Update() { float distance = Vector3.Distance(transform.position, player.position); float scaleZ = Mathf.Clamp(distance * scaleFactor, initialScale.z, maxScale); transform.localScale = new Vector3(initialScale.x, initialScale.y, scaleZ); } } [1]: /storage/temp/205867-new-samplescene-windows-mac-linux-unity-2021320f1.png

Viewing latest article 30
Browse Latest Browse All 168

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>