- Home /
How can I move an object to a (0,0) and scale it to (0,0) at the same time?
How can I move an object to a (0,0) and scale it to (0,0) at the same time? If i try like: transform.position = Vector3(transform.position, Vector3.zero, Time.deltaTime * 3f); transform.localScale = Vector3.Lerp(transform.position, Vector3.zero, 0f); They dont sync I mean they move but they can get to the specific point without scale to (0,0) or they can scale to (0,0) without reaching the specific point. The specific point is (0,0) (Its 2D). Thanks! P.S If I scale the object they x and the y arent the same does that have a fix? Also thanks!
Answer by Behisoft · Oct 20, 2018 at 04:26 PM
First of all, you might need some sort of easing function for making the transition look good. I'm not explaining that here. For both scaling and also moving the object, you first need to define the time in which you want the transition to happen. For example I assume you need this to happen during the course of one second. bool shouldGoAway = false; bool isShown = true; float durationSec = 1; float transitionProgressSec = 1; Vector3 startingScale; Vector3 startingPosition; private void Update() { // this.controller.Update(); if (shouldGoAway) { if (isShown) { startingPosition = transform.position; startingScale = transform.localScale; isShown = false; // We set this here in order to prevent further Update calls from updateing start points } transitionProgressSec += Time.deltaTime; float progress = Mathf.Clamp01(transitionProgressSec / durationSec); // This shows the progress which is used in Lerp method transform.position = Vector3.Lerp(startingPosition, Vector3.zero, progress); transform.localScale = Vector3.Lerp(startingScale, Vector3.zero, progress); // We also want to check if the transition is done if (progress == 1) { shouldGoAway = false; } } }
This should solve your problem.
Thanks for answering. This doesnt work sorry. maybe the Lerp is the problem. Got any other suggestions?
I did: transform.localScale = Vector2.Lerp(transform.localScale, Vector2.zero, 0); transform.position = Vector3.$$anonymous$$oveTowards(transform.position, Vector3.zero, Time.deltaTime * 3f); The only problem is the x and the y of the scales dont match and that dont looks good. Any advices how can i avoid it?