- Home /
How can I make the player step forward on mouse click?
Hello, Unity community. I am making a simple 2D game and i need my player to move forward a bit in the direction of the mouse cursor when you attack using the "Fire2" button (right mouse button), as if taking a step. I got it to work somewhat by using transform.Translate, but the player moves farther depending on how far the mouse is from the player, and this isn't what I want. I think this is because I'm subtracting the players position from the mouse's position to get the direction, but I don't know any other way to get the direction. I was wondering if somebody here can help me out.
Answer by kalen_08 · Jul 20, 2018 at 12:42 AM
If you want it to always be the same distance... After you've got the difference of Vector3 positions then..
var direction = (Vector3b - Vector3a).normalized;
//This returns the vector with a length of 1 unit
I usually just stick to 2D and this is how I always get directions, because yeah there's no function for direction :( hope this solves the problem:)
Thank you so much! This worked perfectly. However, I still have one small problem: I have a mouse cursor thing that follows the real mouse and acts like the mouse would, and it works perfectly except it twitches a little when the player takes the step. Is there a way I can prevent this?
why do you have something which looks like a mouse and follows the mouse but isn't a mouse???
if you want to change what the mouse does then use Cursor.SetCursor(Texture2D texture, Vector2 hotspot, Cursor$$anonymous$$ode cursor$$anonymous$$ode);
https://docs.unity3d.com/ScriptReference/Cursor.SetCursor.html
if you're using it for something else plz explain..
Thank you. I wasn't aware that was even an option. It works well and changes the cursor to what I want, but this creates yet another problem: I have a laser gun that fires from the player in the direction of the mouse pointer and it was working just fine, but now the mouse cursor is a little off-center from the laser unless it's pointing directly to the top-left or bottom-right. Sorry for asking so many questions, it just seems that every time I solve 1 problem, another one is created.
Answer by kog513 · Jul 18, 2018 at 10:13 AM
I never programmed in 2D with unity , I used to creat 2D games using construct , but I think u can just do the same thing I used to do with construct , U can store the position of the mouse compare it to the position of ur player then according to that position u add transform.translate or u can just do it using transfrom.position += new vector2(1,0) if mouving right , (-1,0) if mouving left , and ofc u decide wether mouving left or right according to the sign of Input.MousePosition.x-Player.transform.position.x , or Y not sure , just the horizontal axis , Sorry if this is uselss , just trying to help . I'm Talking about a platform kind of Games . if it's 8 directions then just store both the X and Y and make ur own system with the same Idea
Okay, I got it to work by starting a countdown every time the mouse button is clicked and using transform.Translate in the direction of the mouse while the count down is active. But the problem is that I'm getting the direction by subtracting the player's position from the mouse position, so the player goes further depending on how far away the mouse is from the player, and this is not what I want. $$anonymous$$y game has free movement in all directions and is not just restricted to 2 directions, or even 8, so using Vector2.right and such is not an option for me. How do you suggest I solve my problem?
@SirPepato normalize the direction vector then multiply it by the speed which you want , normalizing a vector means setting it's magnitude to 1 but the direction stays the same , u can normalize a vector by using this : myvector2 = myvector2.normalized I hope this helps .
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
My character moves when i dont want to 0 Answers
How do i maintain the same speed in the air? 1 Answer
Help with a character movement script? 1 Answer
Player Movement 0 Answers