统一 - 基于原点距离改变局部尺度

时间:2017-09-24 17:39:04

标签: object unity3d size

我一直在寻找一种可以调整2D游戏对象大小的方法,具体取决于它与原点的距离。当游戏对象的距离为4个单位时,它的局部尺度应为(1,1,1)。当它到达原点时,它的局部标度应为(0,0,1)。这应该会让人觉得游戏对象越走越远。如果有人知道如何实现这一点,我们将非常感谢你告诉我。

先谢谢,

托米

2 个答案:

答案 0 :(得分:3)

您可以在比例值上使用linear interpolation。从变换局部比例中取[Vector3][1]并传递距离原点的距离。

显示我正在谈论的一些伪代码:

get the transform
in the update figure out the distance from the origin
get the lerped value (Vector3.lerp(new Vector3(1,1,1), new Vector3(0,0,1), distance from center))

添加了示例代码

public class Scaling : MonoBehaviour
{
    private Transform trans;

    void Start ()
    {
        trans = gameObject.transform;
    }

    void Update ()
    {
        float dist = Vector3.Distance(Vector3.zero, transform.position);

        //don't scale if further away than 4 units
        if(dist > 4)
        {
            transform.localScale = Vector3.forward;
            return;
        }

        //work out the new scale
        Vector3 newScale = Vector3.Lerp(Vector3.one, Vector3.forward, dist / 4);
        transform.localScale = newScale;
    }
}

enter image description here

答案 1 :(得分:-1)

检查文档[https://docs.unity3d.com/ScriptReference/Transform-localScale.html][1]

或尝试使用以下方法调整大小:

 #include <stdio.h>
 int main(void) {
     int a,b;
     a = 5;
     b = 18;
     printf("La media aritmética de %d i %d es %d\n", a, b, (a+b)/2);
     return 0;
 }

将它放入if语句中,它应该可以正常工作。