【问题标题】:Errors while changing unity3d movement to drag (c#)将 unity3d 移动更改为拖动时出错(c#)
【发布时间】:2015-04-08 21:30:10
【问题描述】:

我的错误是

  • “UnityEngine.Collider2D”是一个“类型”,但用作“变量”
  • 'UnityEngine.Vector3.Vector3(float, float, float)' 的最佳重载方法匹配有一些无效参数
  • 参数 '1':不能从 'double' 转换为 'float'

    using UnityEngine;
    using System.Collections; 
    
    public class MoveRacket : MonoBehaviour {
        public float speed = 30;
        public string axis = "Vertical";
        public object racket = "Racket";
        public bool touchInput = true;
        public Vector2 touchPos;
    
    
    
    
    
        void FixedUpdate () {
    
            //used to not have anything in parentheses
            //float v = Input.GetAxisRaw (axis);
            float v = Input.GetAxisRaw (axis);
            GetComponent<Rigidbody2D> ().velocity = new Vector2 (0, v) * speed;
            if (Input.touchCount == 1)
            {
                Vector3 wp = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
                Vector2 touchPos = new Vector2(wp.x, wp.y);
                if (Touch = Physics2D.OverlapPoint(touchPos))
                {
                    this.transform.position =  new Vector3 (3.94,wp.y,0);
    
            }
            }
        }
    }
    

【问题讨论】:

    标签: c# unity3d touch 2d drag


    【解决方案1】:

    这里有 2 个问题。第一个是您尝试将 Physics2D.OverlapPoint(touchPos) 的 Collider2D 结果分配给 Touch 的统一结构类型。不太确定你的意图是什么。如果你只是想测试touchPos是否重叠,那么使用这个代码:

    public class MoveRacket : MonoBehaviour {
    public float speed = 30;
    public string axis = "Vertical";
    public object racket = "Racket";
    public bool touchInput = true;
    public Vector2 touchPos;
    
    
    
    
    
    void FixedUpdate () {
    
        //used to not have anything in parentheses
        //float v = Input.GetAxisRaw (axis);
        float v = Input.GetAxisRaw (axis);
        GetComponent<Rigidbody2D> ().velocity = new Vector2 (0, v) * speed;
        if (Input.touchCount == 1)
        {
            Vector3 wp = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
            Vector2 touchPos = new Vector2(wp.x, wp.y);
            if (Physics2D.OverlapPoint(touchPos) != null)
            {
                this.transform.position =  new Vector3 (3.94f,wp.y,0);
    
        }
        }
    }
    }
    

    您的另一个问题是 3.94 的值被视为双精度数据类型,而 vector3 构造函数需要一个浮点数。要将其解释为浮点数,请在数字末尾添加“f”。

    【讨论】:

    • 抱歉,我之前忘记了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-08
    • 2011-09-30
    • 1970-01-01
    • 1970-01-01
    • 2013-04-21
    • 1970-01-01
    相关资源
    最近更新 更多