【问题标题】:Why am I getting this error in Unity3D?为什么我在 Unity3D 中收到此错误?
【发布时间】:2014-08-26 10:34:16
【问题描述】:

这是我得到的错误:

Assets/Scripts/CameraScript.cs(60,39):错误 CS1612:无法修改“UnityEngine.Transform.position”的值类型返回值。考虑将值存储在临时变量中

这是我的代码:

void  Start (){
        thisTransform = transform;

        // Disable screen dimming
        Screen.sleepTimeout = 0;
    }

    void  Update (){
        //Reset playerscore 
        playerScore = settings.playerScore;

        //Smooth follow player character
        if  (target.position.y > thisTransform.position.y)  {           
            thisTransform.position.y = Mathf.SmoothDamp( thisTransform.position.y, 
                                                        target.position.y, ref velocity.y, smoothTime);
        }
    }

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    您不能单独设置thisTransform.position.y 值。改为设置thisTransform.position

    例如:

    if  (target.position.y > thisTransform.position.y)  {           
      thisTransform.position = new Vector3(thisTransform.position.x, 
                                           Mathf.SmoothDamp( thisTransform.position.y, target.position.y, ref velocity.y, smoothTime), 
                                           thisTransform.position.z);
    }
    

    【讨论】:

      【解决方案2】:

      Transform.position.y 在 C# 中是只读的,因此为了修改它,您需要先将 Transform.position 的值存储到临时变量中,更改该变量的值,然后将其分配回 @ 987654323@:

      Vector3 temp = thisTransform.position;
      temp.y = Mathf.SmoothDamp( temp.y, target.position.y, ref velocity.y, smoothTime);
      thisTransform.position = temp;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-10-08
        • 2018-05-13
        • 2018-06-22
        • 2016-01-01
        • 2012-04-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多