2

我正在制作要在 Unity3D 的主相机对象上使用的脚本,以便相机跟随 2D 平台游戏世界中的角色。

我试图将其从 UnityScript 脚本转换为 c#,在第 26 行出现错误:“无法修改值类型返回值‘UnityEngine.Transform.position’。考虑将值存储在临时变量中。”

原始 UnityScript 版本

var cameraTarget : GameObject;
var player : GameObject;

var smoothTime : float = 0,1;
var cameraFollowX : boolean = true;
var cameraFollowY : boolean = true;
var cameraFollowHeight : boolean = false;
var cameraHeight : float = 2.5;
var velocity : Vector2;
private var thisTransform : Transform;

function Start ()
{
  thisTransform = transform;
}

function Update () 
{

if (cameraFollowX)
{
  thisTransform.position.x = Mathf.SmoothDamp (thisTransform.position.x, cameraTarget.transform.position.x, velocity.x, smoothTime);
}

if (cameraFollowY)
{
  thisTransform.position.y = Mathf.SmoothDamp (thisTransform.position.y, cameraTarget.transform.position.y, velocity.y, smoothTime);
}

if (!cameraFollowX & cameraFollowHeight)
{
  camera.transform.position.y = cameraHeight;
}

}

我的 C# 版本

using UnityEngine;
using System.Collections;

public class CameraSmoothFollow : MonoBehaviour {

    public GameObject cameraTarget; // object to look at or follow
    public GameObject player; // player object for moving

    public float smoothTime = 0.1f; // time for dampen
    public bool cameraFollowX = true; // camera follows on horizontal
    public bool cameraFollowY = true; // camera follows on vertical
    public bool cameraFollowHeight = true; // camera follow CameraTarget object height
    public float cameraHeight = 2.5f; // height of camera adjustable
    public Vector2 velocity; // speed of camera movement

    private Transform thisTransform; // camera Transform

    // Use this for initialization
    void Start () {
        thisTransform = transform;
    }

    // Update is called once per frame
    void Update () {
        if (cameraFollowX){
            thisTransform.position.x = Mathf.SmoothDamp (thisTransform.position.x, cameraTarget.transform.position.x, ref velocity.x, smoothTime); // Here i get the error
        }
        if (cameraFollowY) {
        // to do    
        }
        if (!cameraFollowX & cameraFollowHeight) {
        // to do
        }
    }
}

任何帮助将不胜感激。

4

3 回答 3

3

发生此错误是因为Transform.position是值类型(可能是 a struct)。这意味着当您访问 的 X 属性时position,您访问的是 COPY 而不是真实的东西。要分配给 position 属性,您需要创建一个新 Vector3 并将其分配给 position 属性。您的代码将如下所示:

thisTransform.position = new Vector3 (Mathf.SmoothDamp (thisTransform.position.x, cameraTarget.transform.position.x, ref velocity.x, smoothTime), thisTransform.position.y, thisTransform.position.z);

或者更清洁:

float tempX = new Vector3 (Mathf.SmoothDamp (thisTransform.position.x, cameraTarget.transform.position.x, ref velocity.x, smoothTime);
thisTransform.position = new Vector3 (tempX, thisTransform.position.y, thisTransform.position.z);
于 2013-08-28T00:26:40.573 回答
1

您还可以将这个平滑的跟随脚本用于 2d 和 3d 相机,而且非常简单。

using UnityEngine;
using System.Collections;

public class FollowCamera : MonoBehaviour {

public float interpVelocity;
public float minDistance;
public float followDistance;
public GameObject target;
public Vector3 offset;
Vector3 targetPos;
// Use this for initialization
void Start () {
    targetPos = transform.position;
}

// Update is called once per frame
void FixedUpdate () {
    if (target)
    {
        Vector3 posNoZ = transform.position;
        posNoZ.z = target.transform.position.z;

        Vector3 targetDirection = (target.transform.position - posNoZ);

        interpVelocity = targetDirection.magnitude * 5f;

        targetPos = transform.position + (targetDirection.normalized * interpVelocity * Time.deltaTime); 

        transform.position = Vector3.Lerp( transform.position, targetPos + offset, 0.25f);

    }
}
}

从这里的 github 得到它 https://gist.github.com/unity3diy/5aa0b098cb06b3ccbe47

于 2015-02-21T22:17:12.120 回答
1

利用FixedUpdate()

如果Update()是相机抖动就会发生。

于 2016-03-11T10:46:53.803 回答