2017-05-10 20 views
0

私はプレイヤーがクリックされたオブジェクトに向かって動くように2Dのポイントアンドクリックゲームを作っています。ユニティ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); 
     } 
    } 
} 

}

問題は、プレイヤーが唯一のクリックごとに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); 
    } 
} 
関連する問題