Shooting a shell in the direction of the mouse in Unity

I'm in the process of creating a 2D game where the player needs to click to shoot. Wherever the mouse cursor is on the screen, the direction of movement of the projectile is directed. I was able to get shells for creating the instance and for the mouse, but the shell follows the mouse, which is not what I want.

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);
                }
            }
        }
    }
}

I watched several YouTube videos and watched several Unity forums, but the solutions either gave me errors, or they gave me different problems, such as just shooting on one axis or shooting on opposite axes

+4
source share
1 answer

Update() , , .

, , /.

+4

Source: https://habr.com/ru/post/1690119/


All Articles