简体   繁体   English

使用Unity3D将Camera Smooth从UnityScript跟随2D到C#

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

I am making a script to be used on the Main Camera object of Unity3D so the camera follows the character in a 2D platformer world. 我正在制作要在Unity3D的Main Camera对象上使用的脚本,以便相机在2D平台游戏世界中跟随角色。

I tried to translate this from a UnityScript script to c#, I am getting an error in line 26: "Cannot modify a value type return value of 'UnityEngine.Transform.position'. Consider storing the value in a temporary variable." 我试图将其从UnityScript脚本转换为c#,但在第26行中出现错误:“无法修改'UnityEngine.Transform.position'的值类型返回值。请考虑将值存储在临时变量中。”

Original UnityScript Version 原始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;
}

}

My C# Version 我的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
        }
    }
}

Any help will be appreciated. 任何帮助将不胜感激。

This error occurs because Transform.position is of a value type (probably a struct ). 发生此错误的原因是Transform.position是值类型(可能是struct )。 This means that when you access the X property of position , you are accessing a COPY and NOT the real thing. 这意味着,当您访问position的X属性时,您正在访问的是COPY而不是真实的东西。 To assign to the position property, you will need to create a new Vector3 and assign it to the position property. 要分配给position属性,您需要创建一个新的Vector3并将其分配给position属性。 Your code will look something like this: 您的代码将如下所示:

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

or perhaps more cleaner: 或更干净一点:

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

You can also use this smooth follow script for both 2d and 3d camera and its very simple. 您也可以对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);

    }
}
}

got it from github here https://gist.github.com/unity3diy/5aa0b098cb06b3ccbe47 从github上获得它https://gist.github.com/unity3diy/5aa0b098cb06b3ccbe47

Use FixedUpdate() 使用FixedUpdate()

If it's Update() the camera jerking will occur. 如果是Update() ,则会发生照相机震动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM