How can I scale slowly the object to 0,0,0?
You could just modify your Coroutine in following way
IEnumerator ScaleDownAnimation(float time)
{
float i = 0;
float rate = 1 / time;
Vector3 fromScale = transform.localScale;
Vector3 toScale = Vector3.zero;
while (i<1)
{
i += Time.deltaTime * rate;
transform.localScale = Vector3.Lerp(fromScale, toScale, i);
yield return 0;
}
}
here you could just pass time
to this Coroutine
So Here you can call this Coroutine from Start()
as
StartCoroutine(ScaleDownAnimation(1.0f));
So, in this case, it will take exactly 1 second to scale down to Vector3.zero
from its initial scale.
I recommend using AnimationCurve for smoother animations
You could Declare public AnimationCurve curve
in your script and modify that curve from the inspector.
now modify the Code transform.localScale = Vector3.Lerp(fromScale, toScale, i);
to transform.localScale = Vector3.Lerp(fromScale, toScale, curve.Evaluate(i));
I am quite sure you will get the desired result.
Rotate and scale an object with speed over time
The problem was with the transform.rotation
that was not able to change the rotation from -90/270 to new rotation. I instead had to use transform.localRotation
and it worked perfectly.
using UnityEngine;
using System.Collections;
public class ScaleAndRotate : MonoBehaviour {
public int startSize = 3;
public int minSize = 1;
public int maxSize = 6;
public float speed = 2.0f;
private Vector3 targetScale;
private Vector3 baseScale;
private int currScale;
//ROT
public Quaternion targetRotation;
public Quaternion initialRotation;
public bool startRotation = false;
void Start() {
baseScale = transform.localScale;
transform.localScale = baseScale * startSize;
currScale = startSize;
targetScale = baseScale * startSize;
initialRotation = transform.localRotation;
}
void Update() {
transform.localScale = Vector3.Lerp (transform.localScale, targetScale, speed * Time.deltaTime);
if(startRotation == true)
{
transform.localRotation = Quaternion.Slerp(transform.localRotation, targetRotation, Time.deltaTime * speed);
}
else
if(startRotation == false)
{
//Go Back To Initial Rotation
transform.localRotation = Quaternion.Slerp(transform.localRotation, initialRotation, Time.deltaTime * speed);
}
if (Input.GetKeyDown (KeyCode.UpArrow))
{
ChangeSize (true);
startRotation = false;
}
if (Input.GetKeyDown (KeyCode.DownArrow))
{
ChangeSize (false);
startRotation = true;
}
}
public void ChangeSize(bool bigger) {
if (bigger)
{
currScale++;
}
else
{
currScale--;
}
currScale = Mathf.Clamp (currScale, minSize, maxSize+1);
targetScale = baseScale * currScale;
}
}
Change the scale of a gameObject that is not instantiated in a script in unity
The reference to the Transform
component of the very same GameObject
a component is attached to is a built-in property transform
!
In general you could e.g. do
rend.transform.localScale = newLocalScale;
However, in your case your own script is attached to the same GameObject as well so all you need to do is
transform.localScale = newLocalScale;
Btw there is also no need whatsoever to create a new Sprite
instance every frame! Altering the texture will automatically also update the Sprite
based on that texture!
And even then still questionable why you would change all the pixels of a texture every frame ..
Scale GameObject in Unity
It's a property of the transform
component
sprite.transform.localScale = new Vector3(2.0f, 2.0f, 2.0f);
Unity3D: Convert all game objects to same size irrespective of it's scale
Get the mesh filter on each and use Mesh.bounds.size to get their sizes. To make the second object fit inside the first, you'd change the second object's scale based on the different in mesh size.
// If you want it to be 100% accurate, you should multiply RefSize here
// by RefMeshFilter.transform.lossyScale so that the other object's
// size also accounts for the reference object's scale.
var RefSize = RefMeshFilter.bounds.size;
var MySize = MyMeshFilter.bounds.size;
var NewScale = new Vector3(RefSize.x / MySize.x, RefSize.y / MySize.y, RefSize.z / MySize.z);
// I'm un-parenting and re-parenting here as a quick way of setting global/lossy scale
var Parent = MyMeshFilter.transform.parent;
MyMeshFilter.transform.parent = null;
MyMeshFilter.transform.localScale = NewScale;
MyMeshFilter.transform.parent = Parent;
How can I scale the object up down automatic?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Scaling : MonoBehaviour
{
public GameObject objectToScale;
public float duration = 1f;
public Vector3 minSize;
public Vector3 maxSize;
public bool scaleUp = false;
public Coroutine scaleCoroutine;
public bool automatic = false;
public bool coroutineIsRunning = false;
private void Start()
{
objectToScale.transform.localScale = minSize;
}
private void Update()
{
if(automatic)
{
if(!coroutineIsRunning)
{
Scale();
}
}
else
{
if (Input.GetKeyDown(KeyCode.G))
{
Scale();
}
}
}
private Scale()
{
scaleUp = !scaleUp;
if (scaleCoroutine != null)
StopCoroutine(scaleCoroutine);
if (scaleUp)
{
scaleCoroutine = StartCoroutine(ScaleOverTime(objectToScale, maxSize, duration));
}
else
{
scaleCoroutine = StartCoroutine(ScaleOverTime(objectToScale, minSize, duration));
}
}
private IEnumerator ScaleOverTime(GameObject targetObj, Vector3 toScale, float duration)
{
float counter = 0;
Vector3 startScaleSize = targetObj.transform.localScale;
coroutineIsRunning = true;
while (counter < duration)
{
counter += Time.deltaTime;
targetObj.transform.localScale = Vector3.Lerp(startScaleSize, toScale, counter / duration);
if(counter > duration)
coroutineIsRunning = false;
yield return null;
}
}
}
How can I make that the object will scale automatic up/down nonstop?
I'm sorry to say your post is long and contains many redundant codes, so I haven't carefully read it, this is a simple reference about making a zoom animation by script.
float minScale; // Minimum scale value
float maxScale; // Maximum scale value
Transform target; // Target to scale
void Update()
{
float scale = Mathf.PingPong(Time.time, maxScale - minScale) + minScale;
target.localScale = new Vector3(scale, scale, scale);
}
Related Topics
Rounding Up to 2 Decimal Places in C#
Asynchronously Sending Emails in C#
Adding and Removing Users from Active Directory Groups in .Net
How to Make an Installer for My C# Application
How to Send an Email in .Net According to New Security Policies
Cssrewriteurltransform with or Without Virtual Directory
Do I Need to Lock or Mark as Volatile When Accessing a Simple Boolean Flag in C#
Recommend a Library/API to Unzip File in C#
Is Async Httpclient from .Net 4.5 a Bad Choice for Intensive Load Applications
Random Errors Using Wait for Element Clickable Method in Selenium
Using Bindingoperations.Enablecollectionsynchronization
Accelerating Bitmap Grayscale Conversion, Is Openmp an Option in C#
Log4Net, How to Add a Custom Field to My Logging
Create Web Service Proxy in Visual Studio from a Wsdl File
Practical Usage of Virtual Functions in C#
Display Custom Error Page When File Upload Exceeds Allowed Size in ASP.NET MVC