0

我正在制作一个 2D 指向和点击游戏,我希望玩家向点击的对象移动。这是我将玩家移向门的代码:

using UnityEngine;
using System.Collections;

public class MoveOnClick : MonoBehaviour {
public GameObject door;
public GameObject player;
public float speed;
public Vector3 target;

void Update () {
    if (Input.GetMouseButtonDown (0)) {
        RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector3.zero);
        if (hit.collider != null) {
            player.transform.position = Vector3.MoveTowards(player.transform.position, target, speed * Time.deltaTime);
        }
    }
}

}

问题是玩家每次点击只移动一个像素。如果门被点击,我希望玩家一直移动到门。

4

1 回答 1

0

那应该工作:

void Update () {
    if (Input.GetMouseButtonDown (0)) {
        RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector3.zero);  
    target = hit.transform.position;  
    }

    if (hit.collider != null) {
        player.transform.position = Vector3.MoveTowards(player.transform.position, target, speed * Time.deltaTime);
    }
}
于 2017-05-11T12:39:09.337 回答