使用Unity3D从UnityScript到C#的摄影机平滑跟随2D
我正在制作一个脚本,用于Unity3D的主要摄影机对象,以便摄影机跟随2D平台世界中的角色 我试图把它从一个unyScript脚本翻译成C,我在第26行中得到了一个错误:“不能修改UNIYEngEngult.Trime.Posits的值类型返回值。考虑将值存储在一个临时变量中。” 原始UnityScript版本使用Unity3D从UnityScript到C#的摄影机平滑跟随2D,c#,camera,unity3d,unityscript,C#,Camera,Unity3d,Unityscript,我正在制作一个脚本,用于Unity3D的主要摄影机对象,以便摄影机跟随2D平台世界中的角色 我试图把它从一个unyScript脚本翻译成C,我在第26行中得到了一个错误:“不能修改UNIYEngEngult.Trime.Posits的值类型返回值。考虑将值存储在一个临时变量中。” 原始UnityScript版本 var cameraTarget : GameObject; var player : GameObject; var smoothTime : float = 0,1; var ca
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;
}
}
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
}
}
}
我的C#版本
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;
}
}
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
}
}
}
任何帮助都将不胜感激。发生此错误的原因是
转换。位置
属于值类型(可能是结构
)。这意味着当您访问位置的X属性时,您访问的是一个副本,而不是真实的对象。要指定给position属性,您需要创建一个新矢量3并将其指定给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这里得到的
使用fixeupdate()
如果是Update()
则会发生相机抖动。如果它也能与我的3d相机配合使用,将会更有趣。让我查一查。。顺便说一句,谢谢:)这是错误的,FixeUpdate是物理版,你应该在Update()中更新玩家的移动,而不是FixeUpdate()中