簡體   English   中英

使用Unity3D將Camera Smooth從UnityScript跟隨2D到C#

[英]Camera Smooth Follow 2D from UnityScript to C# using Unity3D

我正在制作要在Unity3D的Main Camera對象上使用的腳本,以便相機在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
        }
    }
}

任何幫助將不勝感激。

發生此錯誤的原因是Transform.position是值類型(可能是struct )。 這意味着,當您訪問position的X屬性時,您正在訪問的是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);

您也可以對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

使用FixedUpdate()

如果是Update() ,則會發生照相機震動。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM