Help with trying to make a 2D character move in all four directions via the arrow keys
Hello.
So recently I have set myself a little project to try and recreate a level from the MSX game known as Metal Gear 2 Solid Snake. I've got all the sprites set up, camera placement and 2D settings and such along with the rigidbody2d and colliders where they need to be along with the animations set up and the states of them linked.
What I am struggling with is the script to move the character by pressing any of the four arrow keys. Since the MSX originally was a overhead top down view where you could move in all four directions, I am having trouble trying to set up a script to work with it. Most of the tutorials I have found focus mainly on the side scroller aspect of 2D, which is something I don't need.
I have little experience with scripting so I may be looking at the wrong tutorials. So any help with this would be appreciated. Thanks.
Answer by Ali-hatem · Apr 28, 2016 at 08:26 PM
float H,V;
public float speed;
Rigidbody2D rgb;
void Start(){
rgb = GetComponent<Rigidbody2D>();
}
void Update() {
H = Input.GetAxis("Horizontal");
V = Input.GetAxis("Vertical");
rgb.velocity = new Vector2(H*speed,V*speed);
}
I've looked at your script and I am not getting any errors now with the scripting thankfully. But I still am unable to move the character sprite. It is more or less stuck in animating through the animation rather than moving. Not sure if this is down to something I have done regarding the sprite animation or if I am missing another thing.
have you assigned a value for speed
in inspector ?
Yep speed has a value. But the sprite just goes through it's animations and still doesn't move. Basically this is what happens even with the script for movement applied.
https://www.youtube.com/watch?v=kzOSy_jHRek&feature=youtu.be
Right sorry for the late reply. Just sent the project to you. $$anonymous$$aybe you can give me some guidance on what I am missing here.
Answer by Jessespike · Apr 28, 2016 at 08:27 PM
The concept of Input is usually the same, the implementation might be a bit different, but you should be able to figure out what to do by looking at the sidescroller tutorials.
// top-down movement, using the XZ axises
Vector3 moveVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
transform.Translate(moveVector);
Here I'm using transform.Translate for simplicity, but if your character has a CharacterController component, it might be better to use the Move or SimpleMove functions instead. If the character has a RigidBody, then setting the velocity or adding force might be the way to go.
Your answer
Follow this Question
Related Questions
How to make "choose character" just before the game(2D)? 0 Answers
Problems with player jumping (Welcome to Unity Answers The best place to ask and answer questions ) 0 Answers
How do I make the character rotate with the 2D camera? 0 Answers
RawImage is not showing in GamePlay but showing in Inspector 0 Answers
align player to object location in C#? 0 Answers