上下移动取决于鼠标单击时的位置

时间:2018-07-12 07:54:36

标签: c# unity3d

我有点困境。我正在开发游戏,我需要做一些我不了解的事情。我有一个可通过此脚本上下移动的对象:

from flask import Flask, request
from twilio.twiml.messaging_response import MessagingResponse
 
from tags import get_relevant_tags

app = Flask(__name__)
 
@app.route('/sms', methods=['POST'])
def sms_reply():
    resp = MessagingResponse()
    resp.message("a")
    return str(resp)
 
if __name__ == '__main__':
    app.run()

现在,当我单击“鼠标(0)”时,我想这样做,如果Mouse.y> 0,则对象向上移动,如果Mouse.y位置<0,则对象向下移动

    void FixedUpdate()
{
    if (canClick)
    {
        float moveVertical = Input.GetAxis("Vertical");
        Vector3 movement = new Vector3(0f, moveVertical, 0f);
        Tucan.GetComponent<Rigidbody2D>().velocity = movement * speed;
        Tucan.GetComponent<Rigidbody2D>().position = new Vector3
        (
            -7.5f,
            Mathf.Clamp(Tucan.GetComponent<Rigidbody2D>().position.y, boundary.yMin, boundary.yMax),
            0.0f
        );
    }
}

如何执行与FixedUpdate相同的代码,在Update中运行它,但是要进行检查。并且已经开始依赖Mouse.y。

1 个答案:

答案 0 :(得分:1)

只需将WorldSpace鼠标位置Y与您的GameObjects Y比较

void Update()
{
    Vector3 mouse = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    if (Input.GetMouseButton(0))
    {
        Vector3 movement;
        if (mouse.y > Tucan.transform.position.y)
        {
            movement = Vector3.up;
        }
        else
        {
            movement = Vector3.down;
        }


        Tucan.GetComponent<Rigidbody2D>().velocity = movement * speed;
    }
}

编辑

因为您需要分屏解决方案

  

因此屏幕将其分为两部分,当我单击向上时,它向上移动   反之亦然

void Update()
{
    var mouse = Input.mousePosition;
    mouse.y -= Screen.height / 2;
    if (Input.GetMouseButton(0))
    {
        Vector3 movement;
        if (mouse.y > 0)
        {
            movement = Vector3.up;
        }
        else
        {
            movement = Vector3.down;
        }


        Tucan.GetComponent<Rigidbody2D>().velocity = movement * speed;
    }
}