V
V
Vladimir2020-04-21 17:22:38
C++ / C#
Vladimir, 2020-04-21 17:22:38

Why does subtracting vectors give the correct position for moving an object with the mouse?

Hey! I debut with my question and ask those who know the principle of the behavior of the object in my case to explain.
And so - I embody a prototype from the book "Unity and C #. Gamedev from idea to implementation" called Mission Demolition (a game like Angry Birds).
I will describe the essence:
- I need that when the mouse is hovered over the "projectile launch" zone and the mouse is pressed, the projectile appears and moves behind the mouse (this will regulate the direction and strength of the shot).
- The movement of the projectile should be limited to the radii of the launch zone collider

. In the process of studying, I encountered a misunderstanding ...
There is a code:

private void Update()
    {
        Vector3 mouse2D = Input.mousePosition;
        mouse2D.z = -Camera.main.transform.position.z;
        Vector3 mouse3D = Camera.main.ScreenToWorldPoint(mouse2D); //чекаем координаты 
//мыши

        mouse3D -= lounchPos; //корень моей головной боли !!!

        float maxMagnitude = GetComponent<SphereCollider>().radius; 
//ограничиваем перемещение в радиусе "зоны пуска" lounchPoint
        if (mouse3D.magnitude > maxMagnitude)
        {
            mouse3D.Normalize();
            mouse3D *= maxMagnitude;
        }
        projectile.transform.position = projPos; //переменная чтоб дебаг проще делать
        projPos = mouse3D + lounchPos; //перемещаем объект в новую точку
    }
   
    private void OnMouseDown()
    {
        projectile = Instantiate<GameObject>(projectile);
        projectile.transform.position = lounchPos;
    }


All the code above was taken from the book and I understood it up and down, except for the line.
mouse3D -= lounchPos;
Deciding to write on my own, I simply determined the position of the mouse, limited the mouse vector to the radius of the collider and moved the object there.

BUT, in this case the object moved only half of the collider sphere. In the picture, green marks the hemisphere where the object could move after the mouse, and red marks the zones where the object no longer followed the mouse.
5e9effe85ace1295332258.jpeg

If I added to the code, mouse3D -= lounchPos;then the object behaved as it should.

I am trying to understand all parts of the code from the book so that I can easily solve other problems of a similar format in the future (in general, I want to learn, and not just copy-paste). I have read that subtracting the start position from the mouse point gives me the direction of the movement vector between the mouse and the start point and gives me the distance between those points. But I don't have enough strength to REALIZE how/why this happens. The repetition of linear algebra only confused me more, because, according to the parallelogram rule, when subtracting mouse3D-lounnchPos, a vector of a completely different direction is obtained, and why this subtraction works in the code is not clear to me.
Any resources or simple clarifications would be greatly appreciated. 3 days of self-experimentation have not yet been crowned with success. Thank you!

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question