摄像机跟踪-考虑将值存储在临时变量中

本文关键字:变量 存储 跟踪 摄像机 | 更新日期: 2023-09-27 18:12:10

这一行不能正常工作,它需要一个临时变量。没有它,相机就会平放在地上

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);

摄像机跟踪-考虑将值存储在临时变量中

我不相信你可以这样设置矢量的独立轴,一种方法是

// 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#处理结构体和属性的方式。

不能修改transform。直接定位x和y,而不是这样,你应该创建一个具有所需值的Vector3变量,然后分配你的变换。它的位置。例如:

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