无法将类型“unityengine.vector3”隐式转换为“float”
Posted
技术标签:
【中文标题】无法将类型“unityengine.vector3”隐式转换为“float”【英文标题】:Cannot implicitly convert type 'unityengine.vector3' to 'float' 【发布时间】: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
(或者什么都没有,因为那是无论如何都是默认值)
请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。
【参考方案1】:
根据文档Vector3.Zero
是写Vector3(0, 0, 0)
的简写。并且不是float
类型。您需要相应地使用数据类型,例如transform.position
【讨论】:
以上是关于无法将类型“unityengine.vector3”隐式转换为“float”的主要内容,如果未能解决你的问题,请参考以下文章
SQL Server Management Studio 无法将类型为“System.__ComObject
无法将类型为“System.Collections.Generic.List`1[EPMS.Domain.SingleItem]”的对象强制转换为类型“EPMS
C# 无法将类型为“System.Byte[]”的对象强制转换为类型“System.Data.DataTable
无法将类型“[PHAsset]”的值分配给类型“UIImageView!”