C# 相机跟踪-考虑将值存储在临时变量中
无法使此行正常工作,它需要一个临时变量。没有它,相机就只能平放在地上C# 相机跟踪-考虑将值存储在临时变量中,c#,unity3d,C#,Unity3d,无法使此行正常工作,它需要一个临时变量。没有它,相机就只能平放在地上 transform.position.y = currentHeight; 错误CS1612:无法修改“UnityEngine.Transform.position”的值类型返回值。考虑将值存储在临时变量中 部分代码不是我的,只是尝试从java脚本转换到c-sharp并在我当前的camera脚本中实现 // Calculate the current rotation angles var wantedRotationA
transform.position.y = currentHeight;
错误CS1612:无法修改“UnityEngine.Transform.position”的值类型返回值。考虑将值存储在临时变量中
部分代码不是我的,只是尝试从java脚本转换到c-sharp并在我当前的camera脚本中实现
// 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.position x和y,而应该创建一个具有所需值的Vector3变量,然后将transform.position分配给它。例如:
Vector3 newPosition = new Vector3(0, currentHeight, 0);
transform.position = newPosition;
那肯定会导致同样的问题?不能修改y
?我认为需要给它分配一个新对象:transform.position=newvector3(transform.position.x,currentHeight,transform.position.z)代码>我已经有一段时间没有使用unity了,但我相信这确实有效。需要@Alex_TNT来确认。酷,我想我会检查:)这种混乱是因为Vector3
是一个可变结构transform.position
获取结构的副本,因此必须获取副本,对其进行操作,然后再次设置transform.position
。Unity这样做是出于性能原因,尽管它可能会非常混乱。