unity3d 单击时对象消失/对象不向鼠标移动

bsxbgnwa  于 2022-11-25  发布在  其他
关注(0)|答案(2)|浏览(185)

这是我的剧本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GoToMouse : MonoBehaviour
{

    private Transform tf;
    private bool Selected = false;

    // Start is called before the first frame update
    void Start()
    {
        tf = GetComponent<Transform>();
    }

    private void OnMouseDown()
    {
        if (Selected == false)
        {
            Selected = true;
        }
        if (Selected == true)
        {
            Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            tf.position = mousePos;
        }
    }

    private void OnMouseUp()
    {
        if (Selected == true)
        {
            Selected = false;
        }
        if (Selected == false)
        {

        }
    }

    // Update is called once per frame
    void Update()
    {

        
    }
}

在这个脚本中,我想做两件事。我想让一个对象在被点击时被选中,当你放开鼠标时被取消选中。当一个对象被选中时,我想让它向鼠标光标移动。基本上,你可以用鼠标光标拖动它,然后用物理方法投掷它。
这个脚本有几个问题。
1.每当我点击对象时,它就完全消失了。我没有背景或任何它可能在后面的东西,所以我不知道是什么原因造成的。对象也没有移动到任何地方(我检查了它的变换),所以它看起来像是精灵停止渲染
1.每当我选择它并试图移动它时,它会沿着X轴和Y轴移动不到1个单位,然后停止。由于某种原因,在我放开鼠标之前,它会取消选择自己或停止移动。我不知道为什么会这样,因为取消选择对象的唯一方法是放开鼠标。
这是一个unity 2D项目顺便说一句,这个脚本是我正在做的游戏的 Backbone.js 。请帮助!
谢谢你的好意。

ghg1uchk

ghg1uchk1#

我把tf理解为一个需要移动的角色。
根据OnMouseDown函数,
当用户在碰撞器上按下鼠标按钮时,将调用OnMouseDown。
Doc Link here
你需要一个背景,脚本可能会附加到它。从背景中获取鼠标位置。然后将位置设置为tf(字符)。2D游戏的z位置应该总是0。当然,你可以根据需要改变背景的大小。
图像和脚本可以更好地解释。

下面是GoToMouse.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GoToMouse : MonoBehaviour
{
    public Transform tf;
    private bool Selected = false;

    // Start is called before the first frame update
    void Start()
    {
        // the tf is drag and drop from unity
        //tf = GetComponent<Transform>();
    }

    private void OnMouseDown()
    {
        if (Selected == false)
        {
            Selected = true;
        }
        if (Selected == true)
        {
            Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            tf.position = new Vector3(mousePos.x,mousePos.y,tf.position.z); // notice the tf.position.z
        }
    }

    private void OnMouseUp()
    {
        if (Selected == true)
        {
            Selected = false;
        }
    }

}
bvuwiixz

bvuwiixz2#

最后我终于弄明白了,我不知道我做错了什么,但这个新剧本看起来很好用。

using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;

public class MoveTowards : MonoBehaviour
{
    private Rigidbody2D rb;
    public float force = 1f;
    private bool selected = false;

    public void Awake()
    {
        //Get rigidbody from the gameobject this script is attatched to
        rb = GetComponent<Rigidbody2D>();
    }

    public void OnMouseDown()
    {
        //become selected when clicked
        selected = true;
    }

    public void OnMouseUp()
    {
        //become deselected when you let go of left click
        selected = false;
    }

    void Update()
    {
        if (selected == true)
        {
            //move towards mouse if selected
            Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            Vector3 dir = (mousePos - transform.position);
            dir.z = 0.0f;
            Vector3 dirNormalized = dir.normalized;
            Vector2 relativePos = mousePos - gameObject.transform.position;
            rb.AddForce(relativePos * force);
            
        }

    }

}

相关问题