【问题标题】:Making a sprite move进行精灵移动
【发布时间】:2022-10-14 03:10:51
【问题描述】:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UnitMove : MonoBehaviour
{
    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Vector3 MousePos = Input.mousePosition;
            GetComponent<Transform>().position = (MousePos);
        }
    }
}

上面是我的代码,每次我构建和运行游戏时,精灵都不会到达我点击鼠标的位置,而是消失。

【问题讨论】:

  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如它目前所写的那样,很难准确地说出你在问什么。

标签: unity3d


【解决方案1】:

Input.mousePosition 提供 screen coordinate。范围从 (0,0) 到 (Screen.width, Screen.height)。 Transform 组件使用世界坐标。单击精灵后尝试查看其位置值,看看XY 值是否看起来像像素计数。

使用Camera.ScreenToWorldPoint() 将屏幕坐标转换为世界空间。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-20
    • 2021-11-30
    相关资源
    最近更新 更多