A
A
alex4answ2019-07-06 16:55:37
Unity
alex4answ, 2019-07-06 16:55:37

Moving the flying object left-right with the mouse?

Good afternoon, there is a cube that moves along the Z and Y coordinates, the movement is set by applying a force (addForce), the camera is attached to the object.
I need to move this flying object left and right by touch / swipe, I don’t quite know how to do it, but for now I’m thinking like this:

Скрипт на объекте, сила прикладывается по клику на кнопку.
Update(){
 if(Input.GetMouseButtonDown(0)){
      transform.position = new Vector3(Input.mousePosition.x, transform.position.y, transform.position.z);
  }
}

But it seems to me a wild crutch, to change the position every frame, when the position is already calculated by the engine due to the application of force.
+ it just doesn't work, since Input.mousePosition.x is screen dependent and has a very large value.
Please, any advice/direction would be greatly appreciated.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Gaydak, 2019-07-08
@alex4answ

in principle, the approach and thoughts are more or less correct.
The only thing you usually need to take is not the coordinate of the mouse or wheelbarrow, but its deviation.
depending on what kind of mechanics you have .. by pressing or always ..
conditionally if always, then we look at what coordinates the mouse has.
How far are they from the middle of the screen.
We also know the screen size.
and voila..we have 10 percent to the right of the middle..or 95 percent of the middle of the screen to the left.
from here we take the speed factor. and direction)
conditionally from - 1 to 1. where (-1) = move as quickly as possible to the left. , and 1 - respectively to the right)
and something like
where SpeedFactor is the same direction from -1 to 1. and playerSpeed ​​is just an additional multiplier for easy control in the editor.
multiplying by Time.deltaTime allows for adequate control. smooth regardless of the frame rate)
if you had "shift by N pixels each frame" before.
now there will be a uniform "shift by N pixels per second", which is much more convenient to perceive and behaves more adequately))
but about movement. here is a separate issue. if moving forward by force, then in principle you can try to move the force by adding another one to the right or left.
but you have to take into account that without friction and contact, she herself will not stop acting)
so when the input has reached the edge of the strip (let's say the border of the screen) you need to cut off the lateral speed with the handles.
I directly control the velocity of the Rigitbody.
In principle, nothing prevents the movement and leave it as it is, just changing the position. just simply replace the direct reference to the transform with https://docs.unity3d.com/ScriptReference/Rigidbody...
read the difference. in the dock, more or less everything is described)
I hope it became at least a little clearer)))
how to find the screen size. and other similar things - google I think it will come out)
good luck.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question