- Home /
how to make my character walk automatically and when pressing a button change the direction
I am a beginner and I need help, I want my character to walk automatically and change direction when pressing a single button and when pressing it again it is reversed
Do you have any of this working yet? If you have the walking part working, changing direction might be as simple as making a velocity negative. If you don't have any of it, there are multiple parts to this question.
Answer by Gustard · Sep 16, 2020 at 08:36 PM
You will need to create a float or an int hooked up to a bool. In other words, a number that is attached to a true or false statement. your code will look something like this.
public float speed = 4f;
public bool forward;
void update()
{
if(Input.GetButtonDown("Fire1"))
{
if (forward == true)
{
speed = -4f;
forward = false;
}
else
{
speed = 4f;
forward = true;
}
}
}
I don't know how much of a beginner you are, but if you don't know how to make a character move in the first place, I highly suggest you look up "Character Movement Unity3d" on youtube. There are a lot of "Introduction into coding" tutorials out there. Best of luck!
Yeah, but this script won't actually move your character, just an example of how to control the movement. If you don't know how to actually move your character, you should search it on youtube. There are plenty of videos that go through the process step by step for both unity3d and unity2d.
Answer by DaughterGiraffe · Sep 19, 2020 at 09:12 AM
The blog was absolutely fantastic! Lot of great information which can be helpful in some or the other way. Keep updating the blog, looking forward for more contents…Great job, keep it up MyAARPMedicare
Your answer
Follow this Question
Related Questions
How do I keep my character facing the direction of travel after movement stops? 1 Answer
4 way direction movement using a Character Controller. 1 Answer
Character rotating to previous rotation 1 Answer
4 Direction Movement Problem 1 Answer
Enemy AI Movement one axis at a time (8 directional - 2.5d) towards player/target 2 Answers