Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by callumf · Apr 28, 2016 at 05:28 PM · c#2d gamecharacter

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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);
 }
Comment
Add comment · Show 16 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image callumf · Apr 28, 2016 at 09:30 PM 0
Share

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.

avatar image Ali-hatem callumf · Apr 28, 2016 at 10:12 PM 0
Share

have you assigned a value for speed in inspector ?

avatar image callumf Ali-hatem · Apr 28, 2016 at 10:20 PM 0
Share

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

Show more comments
Show more comments
avatar image Ali-hatem callumf · Apr 30, 2016 at 12:17 PM 0
Share

" ali_hatem1983@hotmail.com "

avatar image callumf Ali-hatem · May 03, 2016 at 01:11 PM 0
Share

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.

Show more comments
Show more comments
avatar image
0

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);

Unity - Input.GetAxis

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

158 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges