- Home /
How do you aim towards mouse when two objects are moving?
I'm trying to build a fairly simple 2D sidescroller, where the player does not control the actual character but helps them along by tossing objects at them. However, I've hit a problem - when using the mouse to target a section of screen and two objects are moving, there is a lag time where the object will fall behind the player and character and not to the relative position.
I've gotten this far:
// Launch Position
Vector3 cameraPosition = Camera.main.transform.position;
// Mouse Position
Vector3 pos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 12f));
GameObject _object = items[i];
// Create Item //
GameObject _item = Instantiate(_object, cameraPosistion, Quaternion.identity) as GameObject;
But I'm utterly stumped on what to do next. The items have rigidbodies attached and use physics.
Answer by AlvinHerawan · Oct 31, 2012 at 06:45 AM
Hi Teon,
I think it is logical for the object thrown to be 'left behind' since both the camera and the character you are aiming for is always moving.
There are 2 possible methods I can think of for now. First one is to make the camera and character walking on the spot and animate all the backgrounds instead. This will still create the illusion of moving without literally moving them in the scene.
Secondly, by instantiating the thrown object as a child of either the camera or the character, which has been described here.
Best of luck!
I'm not sure if your second approach will work with physics simulated (thrown) objects, maybe saving/tagging all objects and then updating their positions along with the camera's one would be the better choice :)
@Teonnyn if you plan to have physic simulated objects in the background Alvin's first approach wouldn't be a good choice, because this way the whole level is forced to be simulated.
Which type of method would be best for a Distance/Launch style game?
Jan is right, my second suggestion will most probably be not working. Thanks for enlightening.
The best bet would be to move the background ins$$anonymous$$d, then.
I decided to take a new approach.. it wasn't working overall.
Your answer
![](https://koobas.hobune.stream/wayback/20220613083123im_/https://answers.unity.com/themes/thub/images/avi.jpg)