【问题标题】:Errors with changing to drag in Unity 2D ( C#)在 Unity 2D (C#) 中更改为拖动时出错
【发布时间】:2015-04-08 13:26:30
【问题描述】:

我的错误是 Collider2d 是一种类型,但用作变量。 无法修改 transform.position 的返回值,因为它不是变量。 无法将类型 vector2 隐式转换为浮点数。

这是我的代码

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

        }
        }
    }
}

【问题讨论】:

    标签: c# android unity3d touch draggable


    【解决方案1】:

    您无法将值设置为 position.y/x/z,这是您遇到的错误。所以而不是

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

    使用这个

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

    如果你希望 z 和 x 为零,但你明白了。

    【讨论】:

    • 谢谢,虽然我在 2d 中也会修复 collider2d 错误,所以我应该在那里有 0 代表 z。
    • 即使你在 2D 中,变换对象仍然有 Z 属性(它是一个向量 3:docs.unity3d.com/ScriptReference/Transform-position.html)所以你可以设置它,或者你想要的值
    • 谢谢,你知道我应该如何处理 collider 2d 错误
    • 当然。您在这里使用 Collider2D 作为变量: if (Collider2D == Physics2D.OverlapPoint(touchPos)) 您调用的方式是错误的。我认为您想要做的是知道对象的对撞机是否被触摸点击中。我建议您抓住对象的对撞机并检查该对撞机,以获取更多信息:docs.unity3d.com/ScriptReference/Collider2D.OverlapPoint.html ex: getcomponent().OverlapPoint(touchpos) 将返回 true 或 false
    猜你喜欢
    • 1970-01-01
    • 2016-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多