改变unity3d运动到拖动时的错误(c#)

时间:2015-04-08 21:30:11

标签: c# unity3d touch 2d drag

我的错误是

  • ' UnityEngine.Collider2D'是一种'类型'但用作变量'
  • 最佳重载方法匹配' UnityEngine.Vector3.Vector3(浮点数,浮点数,浮点数)'有一些无效的论点
  • 参数' 1':无法转换为' double'到'漂浮'

    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);
    
            }
            }
        }
    }
    

1 个答案:

答案 0 :(得分:0)

这里有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);

    }
    }
}
}

你的另一个问题是,当vector3构造函数需要float时,3.94的值被视为double数据类型。要将其解释为浮点数,请添加&#34; f&#34;到数字的末尾。