将 unity3d 移动更改为拖动时出错 (c#)
Errors while changing unity3d movement to drag (c#)
我的错误是
- 'UnityEngine.Collider2D' 是 'type',但用法与 'variable'
相同
- '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);
}
}
}
}
您的另一个问题是 3.94 的值被视为双精度数据类型,而 vector3 构造函数需要一个浮点数。要将其解释为浮点数,请在数字末尾添加 "f"。
我的错误是
- 'UnityEngine.Collider2D' 是 'type',但用法与 'variable' 相同
- '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);
}
}
}
}
您的另一个问题是 3.94 的值被视为双精度数据类型,而 vector3 构造函数需要一个浮点数。要将其解释为浮点数,请在数字末尾添加 "f"。