簡體   English   中英

將unity3d運動更改為拖動時出錯(C#)

[英]Errors while changing unity3d movement to drag (c#)

我的錯誤是

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

您在這里有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構造函數期望浮點數時,3.94的值被視為雙精度數據類型。 要將其解釋為浮點數,請在數字末尾添加“ f”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM