我的错误是
参数' 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);
}
}
}
}
答案 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;到数字的末尾。