2017-12-01 1 views
4

나는 플레이어가 클릭하여 쏠 필요가있는 2D 게임을 제작 중입니다. 화면상의 마우스 커서가 움직이는 방향은 발사체가 움직이는 방향입니다. 나는 발사체를 인스턴스화하고 마우스 위치로 가져올 수 있었지만 발사체는 내가 원하는 바가 아닌 마우스를 따라 간다.Unity에서 마우스 위치를 향해 발사체 쏘기

public class LetterController : MonoBehaviour { 

    private List<GameObject> letters = new List<GameObject>(); 
    public GameObject letterPrefab; 
    public float letterVelocity; 

    // Use this for initialization 
    private void Start() 
    { 
    } 

    // Update is called once per frame 
    void Update() 
    { 
     Vector3 direction = Camera.main.ScreenToWorldPoint(Input.mousePosition); 

    if (Input.GetMouseButtonDown(0)) 
    { 
     GameObject letter = (GameObject)Instantiate(letterPrefab, transform.position, Quaternion.identity); 
     letters.Add(letter); 
    } 

    for (int i = 0; i < letters.Count; i++) 
    { 
     GameObject goLetter = letters[i]; 

     if (goLetter != null) 
     { 
      goLetter.transform.Translate(direction * Time.deltaTime * letterVelocity); 

      Vector3 letterScreenPosition = Camera.main.WorldToScreenPoint(goLetter.transform.position); 
      if (letterScreenPosition.y >= Screen.height + 10 || letterScreenPosition.y <= -10 || letterScreenPosition.x >= Screen.width + 10 || letterScreenPosition.x <= -10) 
      { 
       DestroyObject(goLetter); 
       letters.Remove(goLetter); 
       } 
      } 
     } 
    } 
} 

나는 몇 YouTube 동영상을 시청하고 몇 유니티 포럼에서하는 듯했지만 솔루션은 나에게 오류를 준하거나, 그들은 단지 반대 축에 하나 개의 축에 촬영하거나 촬영처럼 나에게 다른 문제를 준

답변

4

귀하의 의견에 따라 Update()는 프레임마다 한 번 호출되며, 각 프레임 내에서 그 시점에 마우스의 위치를 ​​얻으므로 항상 새로운 마우스 위치로 기울어 져 있습니다.

그래서해야 할 일은 액션이 시작될 때 마우스 위치를 가져 와서 작업이 완료/취소 될 때까지 사용하는 것입니다.

+1

어젯밤에 완전히 그걸 놓쳤습니다. 감사! – rocky

관련 문제