Answer the question
In order to leave comments, you need to log in
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;
}
mouse3D -= lounchPos;
mouse3D -= lounchPos;
then the object behaved as it should.mouse3D-lounnchPos
, a vector of a completely different direction is obtained, and why this subtraction works in the code is not clear to me. Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question