【问题标题】:Cannot implicitly convert type 'unityengine.vector3' to 'float'无法将类型“unityengine.vector3”隐式转换为“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 (或者什么都没有,因为那是无论如何都是默认值)
  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。

标签: c# unity3d


【解决方案1】:

根据文档Vector3.Zero 是写Vector3(0, 0, 0) 的简写。并且不是float 类型。您需要相应地使用数据类型,例如transform.position

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-02
    相关资源
    最近更新 更多