在 Unity 2D ( C#) 中更改为拖动时出错

Errors with changing to drag in Unity 2D ( C#)

我的错误是 Collider2d 是一种类型,但像变量一样使用。 无法修改 transform.posistion 的 return 值,因为它不是变量。 无法将类型 vector2 隐式转换为 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 (Collider2D == Physics2D.OverlapPoint(touchPos))
            {
            this.transform.position.y =  new Vector3 (wp.y,0);

        }
        }
    }
}

您不能为位置设置值。y/x/z,这就是您遇到的错误。所以而不是

this.transform.position.y =  new Vector3 (wp.y,0);

使用这个

this.transform.position =  new Vector3 (0,wp.y,0);

也就是说,如果您希望 z 和 x 为零,但您明白了。