【发布时间】:2022-01-15 14:58:39
【问题描述】:
我有一些代码跟随相机,我的错误是:
Assets/Scripts/CameraFollow.cs(15,46):错误 CS0029:无法将 UnityEngine.Vector3' 类型隐式转换为 float'
Assets/Scripts/CameraFollow.cs(25,58):错误 CS0103:名称“camWidth”在当前上下文中不存在
这是我的代码:
using System.Collections;
using System.Collections.Generic
using UnityEngine;
public class CameraFollow : MonoBehaviour {
public Transform target;
public Transform leftBounds;
public Transform rightBounds;
public float smoothDampTime = 0.15f;
private float smoothDampVelocity = Vector3.zero;
private float camHeight, camWidht, levelMinX, levelMaxX;
// Use this for initialization
void Start () {
camHeight = Camera.main.orthographicSize * 2;
camWidht = camHeight * Camera.main.aspect;
float leftBoundsWidth = leftBounds.GetComponentInChildren<SpriteRenderer> ().bounds.size.x / 2;
float rightBoundsWidth = rightBounds.GetComponentInChildren<SpriteRenderer> ().bounds.size.x / 2;
levelMinX = leftBounds.position.x + leftBoundsWidth + (camWidth/2);
levelMaxX = rightBounds.position.x - rightBoundsWidth - (camWidth/2);
}
// Update is called once per frame
void Update () {
if (target){
float targetX = Mathf.Max (levelMinX, Mathf.Min (levelMaxX, target.position.x));
float x = Mathf.SmoothDamp(transform.position.x, targetX, ref smoothDampVelocity, smoothDampTime);
transform.position = new Vector3(x, transform.position.y, transform.position.z);
}
}
}
【问题讨论】:
-
camWidht中有一个类型!=camWidth是的,float smoothDampVelocity = Vector3.zero;没有多大意义......应该只是0(或者什么都没有,因为那是无论如何都是默认值) -
请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。