Hi, I have been trying to implement a kind of recoil mechanic in my 2D game but onto a player, not a gun. Currently I’ve got a kind of top-down shooter gun set up, so my question is how I could get my player to move in the direction opposite of where mouse is pointing towards. Note it should always move for a few tiles, e. g. 2 units. Hope my question is clear! ![]()
it would help if you’ll be more specific about the way the mouse pointer controls the player movement, Do you want to click a mouse button pull the mouse back, release button click, and then the character is moved in the opposite direction? like in angry birds?
or just point the mouse somewhere press a key nd the character moves?
would also help if you share the code you have now and what id does wrong
To answer the first question, it is basically like the shooting mechanic recoil, but with no bullets, if you know what I mean.
Here’s a video of what I want
When it comes to code, I am very sorry but there’s nothing I can showcase, not because I haven’t tried but because I am new to Unity. None of the solutions I have come up seem to work at all, so I am actually looking for a solution to the mechanic not the problem in my code. Thanks for your reply anyways!
- get mouse position
- convert it to world position
- when player fires get the direction from mouse world space position to the character
- move the character in that direction the distance you want to move it
Seems logical, I will give it a shot, I have already done most of the stuff so should not be a problem. Thank you so much!
It’s me again. Everything is besides moving player the distance I want to. How am I supposed to do it, say “dir” is the direction. I’m not sure if AddForce is a logical way to do it, or perhaps velocity should be what I am doing? Translate won’t work though, since I am working with physics, so I have no idea what to use. Thanks and sorry for me being such a noob.
It really depend on your game, like you said that the recoil moves the character 2 tile, if this distance is important for the game design because of tactics or somthing like that i would go with moving it to that specific location with translate or lerping to the new position.
In this case you would need to set the rigidbody to kinematic for the period of time that you are moving it, or disabeling it and re-enabling it once the novement is done.
Another option is If the recoil is for esthetics reasons, I would go with addforce
@NanushTol I finally got it working using translate. Thank you so much for all the efforts! ![]()