【问题标题】:Camera Follow - Consider storing the value in a temporary variable相机跟随 - 考虑将值存储在临时变量中
【发布时间】:2015-09-11 01:12:35
【问题描述】:

无法让这条线正常工作,它需要一个临时变量。没有它,相机会平放在地面上

transform.position.y = currentHeight;

错误 CS1612:无法修改“UnityEngine.Transform.position”的值类型返回值。考虑将值存储在临时变量中

部分代码不是我的,只是试图从java脚本转换为c-sharp并在​​我当前的相机脚本中实现

 // Calculate the current rotation angles
 var wantedRotationAngle = target.eulerAngles.y;
 var wantedHeight = target.position.y + height;
 var currentRotationAngle = transform.eulerAngles.y;
 var currentHeight = transform.position.y;
 // Damp the rotation around the y-axis
 currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
 // Damp the height

 currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);
 // Convert the angle into a rotation

 var currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);
 // Set the position of the camera on the x-z plane to:
 // distance meters behind the target
 transform.position = target.position;
 transform.position -= currentRotation * Vector3.forward * distance;
 // Set the height of the camera

 transform.position.y = currentHeight;

 // Always look at the target
 transform.LookAt (target);

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    我不相信您可以像那样设置矢量的单独轴,您可以这样做的一种方法是

    // Calculate the current rotation angles
    var wantedRotationAngle = target.eulerAngles.y;
    var wantedHeight = target.position.y + height;
    var currentRotationAngle = transform.eulerAngles.y;
    var currentHeight = transform.position.y;
    // Damp the rotation around the y-axis
    currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
    // Damp the height
    
    currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);
    // Convert the angle into a rotation
    
    var currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);
    // Set the position of the camera on the x-z plane to:
    // distance meters behind the target
    transform.position = target.position;
    transform.position -= currentRotation * Vector3.forward * distance;
    // Set the height of the camera
    
    Vector3 temp = transform.position; //Get the current vector the transform is at
    temp.y = currentHeight; //assign the new value to the Y axis
    transform.position = temp; //replace the existing vector with the new one we just modified.
    
    // Always look at the target
    transform.LookAt (target);
    

    所以我们将您的transform.position.y = currentHeight; 行替换为

    Vector3 temp = transform.position; //Get the current vector the transform is at
    temp.y = currentHeight; //assign the new value to the Y axis
    transform.position = temp; //replace the existing vector with the new one we just modified.
    

    这仅仅是因为我相信 C# 处理结构和属性的方式。

    【讨论】:

    • 这肯定会导致同样的问题吗?那个y不能修改?我认为它需要分配一个新对象:transform.position = new Vector3(transform.position.x, currentHeight, transform.position.z);
    • 我已经有一段时间没有使用统一了,但我相信这确实有效。需要@Alex_TNT 确认。
    • 酷,我想我会检查 :)
    • 这种混淆是因为Vector3 是一个可变结构。 transform.position 获取结构的副本,因此您必须获取副本,对其进行操作,然后再次设置 transform.position。 Unity 这样做是出于性能原因,尽管它可能会非常令人困惑。
    【解决方案2】:

    您不能直接修改 transform.position x 和 y,您应该创建一个具有所需值的 Vector3 变量,然后将您的 transform.position 分配给它。例如:

     Vector3 newPosition = new Vector3(0, currentHeight, 0);
     transform.position = newPosition;   
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 2019-10-29
      • 2015-05-31
      • 1970-01-01
      • 2016-06-18
      • 2021-11-29
      相关资源
      最近更新 更多