EloiStree / HelloUnityKeywordForJunior

List of word nice to learn for Unity3D
0 stars 0 forks source link

Keyword: Scale #149

Open EloiStree opened 4 weeks ago

EloiStree commented 4 weeks ago

image

EloiStree commented 4 weeks ago

🤖

1. Setting Scale Directly

// Set scale to specific values
transform.localScale = new Vector3(1, 2, 3);

// Set uniform scale
transform.localScale = Vector3.one * 2;

2. Scaling Object Over Time (Lerp)

// Smoothly scale to a target scale using Lerp
transform.localScale = Vector3.Lerp(transform.localScale, targetScale, scalingSpeed * Time.deltaTime);

3. Uniform Scaling

// Increase scale uniformly
transform.localScale += Vector3.one * scalingSpeed * Time.deltaTime;

// Decrease scale uniformly
transform.localScale -= Vector3.one * scalingSpeed * Time.deltaTime;

4. Non-Uniform Scaling

// Scale only along the X axis
transform.localScale += new Vector3(scalingSpeed * Time.deltaTime, 0, 0);

// Scale only along the Y axis
transform.localScale += new Vector3(0, scalingSpeed * Time.deltaTime, 0);

// Scale only along the Z axis
transform.localScale += new Vector3(0, 0, scalingSpeed * Time.deltaTime);

5. Scaling with Input

// Scale up or down with arrow keys or WASD
float scaleX = Input.GetAxis("Horizontal") * scalingSpeed * Time.deltaTime;
float scaleY = Input.GetAxis("Vertical") * scalingSpeed * Time.deltaTime;

transform.localScale += new Vector3(scaleX, scaleY, 0);

6. Resetting Scale

// Reset scale to its original size
transform.localScale = Vector3.one;

7. Scaling Relative to Another Object

// Scale to match another object's scale
transform.localScale = targetObject.transform.localScale;

8. Clamping Scale

// Clamp scale to stay within certain limits
transform.localScale = new Vector3(
    Mathf.Clamp(transform.localScale.x, minScale, maxScale),
    Mathf.Clamp(transform.localScale.y, minScale, maxScale),
    Mathf.Clamp(transform.localScale.z, minScale, maxScale)
);

9. Smooth Scaling with Coroutines

// Coroutine to smoothly scale an object over time
IEnumerator ScaleOverTime(Vector3 targetScale, float duration) {
    Vector3 startScale = transform.localScale;
    float elapsedTime = 0;

    while (elapsedTime < duration) {
        transform.localScale = Vector3.Lerp(startScale, targetScale, elapsedTime / duration);
        elapsedTime += Time.deltaTime;
        yield return null;
    }

    transform.localScale = targetScale;
}

10. Scaling Based on Distance from Camera

// Scale an object based on its distance from the camera
float distance = Vector3.Distance(transform.position, Camera.main.transform.position);
transform.localScale = Vector3.one * (distance / baseDistance);

11. Scaling with Physics (Rigidbody)

// Apply force to scale a Rigidbody (not a common use case, but possible)
rigidbody.AddForce(Vector3.one * scalingForce);

// Adjust scale based on Rigidbody velocity (if needed for a special effect)
transform.localScale = Vector3.one * rigidbody.velocity.magnitude;

12. Using Vector3.Scale for Element-wise Scaling

// Scale object element-wise by another vector
transform.localScale = Vector3.Scale(transform.localScale, new Vector3(1.1f, 1.0f, 0.9f));

13. Scaling with Animation

// Trigger scaling through an animation clip
animator.SetTrigger("ScaleUp");

// Use animation curves for smooth scaling
public AnimationCurve scalingCurve;

void Update() {
    float scale = scalingCurve.Evaluate(Time.time);
    transform.localScale = new Vector3(scale, scale, scale);
}

14. Pulsating or Bouncing Scale Effect

// Create a pulsing scale effect
transform.localScale = Vector3.one * (1 + Mathf.Sin(Time.time * pulseSpeed) * pulseMagnitude);

15. Scaling Along with Object’s Rotation

// Adjust scale based on object's rotation (e.g., to create a stretch effect)
float scaleFactor = Mathf.Abs(transform.rotation.eulerAngles.y) / 90.0f;
transform.localScale = new Vector3(1, 1, scaleFactor);

16. Using Vector3.one for Uniform Scaling

// Set a uniform scale using Vector3.one
transform.localScale = Vector3.one * uniformScaleValue;

17. Incremental Scaling in Update Loop

// Incrementally scale object up or down
if (shouldScaleUp) {
    transform.localScale += Vector3.one * scalingSpeed * Time.deltaTime;
} else {
    transform.localScale -= Vector3.one * scalingSpeed * Time.deltaTime;
}

18. Using Vector3.Scale with Multiple Objects

// Apply the same scaling to a group of objects
foreach (Transform child in transform) {
    child.localScale = Vector3.Scale(child.localScale, new Vector3(1.2f, 1.2f, 1.2f));
}