- Home /
Transform scale evenly from center to specific position
Hi guys, I have one canvas (i use getworldcorners to draw the red gizmoz sphere) which I want to transform the scale evenly to a specific position (yellow sphere) using a script which means the end result would be the red sphere of the right bottom will scale up to the yellow sphere. May I know how? Thanks.
Red Sphere
World Corner 0 : (-4.0, -5.1, 30.0)
World Corner 1 : (-4.0, 5.1, 30.0)
World Corner 2 : (4.0, 5.1, 30.0)
World Corner 3 : (4.0, -5.1, 30.0)
Yellow Sphere
BottomLeft (-9.1, -11.3, 30.0)
TopRight (9.1, 11.4, 30.0)
Answer by MusapKahraman · Apr 28, 2020 at 10:16 AM
@TrueXRCo The following script takes two transforms as guides and changes the position, scale, and rotation according to these guides. I hope this is what you want. This will function after you start playing. You need to replace the Update function if you want it to work in the editor.
using UnityEngine;
[RequireComponent(typeof(Canvas))]
public class CanvasEvenlyScale : MonoBehaviour
{
#pragma warning disable 0649
[SerializeField] private Transform guideBottomLeft;
[SerializeField] private Transform guideTopRight;
#pragma warning restore 0649
private Canvas _canvas;
private RectTransform _rect;
private void Start()
{
_canvas = GetComponent<Canvas>();
_canvas.renderMode = RenderMode.WorldSpace;
_canvas.worldCamera = Camera.main;
_rect = GetComponent<RectTransform>();
_rect.pivot = Vector2.zero;
}
private void Update()
{
_rect.position = guideBottomLeft.position;
_rect.localScale = GetGuidedCanvasScale();
_rect.rotation = Quaternion.Euler(new Vector3(0, GetGuidedRotation(), 0));
}
private Vector3 GetGuidedCanvasScale()
{
var guideBottomLeftPosition = guideBottomLeft.position;
var guideTopRightPosition = guideTopRight.position;
var height = guideTopRightPosition.y - guideBottomLeftPosition.y;
var distanceX = guideTopRightPosition.x - guideBottomLeftPosition.x;
var distanceZ = guideTopRightPosition.z - guideBottomLeftPosition.z;
var width = HypotenuseLength(distanceX, distanceZ);
var rect = _rect.rect;
return new Vector3(width / rect.width, height / rect.height, 1);
}
private static float HypotenuseLength(float sideALength, float sideBLength)
{
return Mathf.Sqrt(sideALength * sideALength + sideBLength * sideBLength);
}
private float GetGuidedRotation()
{
var position = guideTopRight.position - guideBottomLeft.position;
var widthVector = new Vector2(position.x, -position.z);
return Vector2.SignedAngle(Vector2.right, widthVector);
}
}
Hi @$$anonymous$$usapKahraman, I did not need the position and rotation transformation but the scaling part is exactly what I am looking for. Thank you so much!
You are welcome! I really liked your idea, I'm glad I could help.
Your answer
Follow this Question
Related Questions
I tried to make a box to select units like you do in windows 1 Answer
Position and scale GameObject according to screen size 2 Answers
Change Text Scale on a Canvas Child from a script. 1 Answer
Multiple Cars not working 1 Answer
Change position of a text prefab added to a world space canvas via scripting 0 Answers