Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by LelouchLamperouge · May 24, 2015 at 12:18 PM · c#2dplayergame

2D Sidescroller Issues Please help.

Hello. I am new to Unity and I am having trouble getting a way of controlling my character when making a 2D Sidescroller type game. The main trouble I have is with scripting all help and suggestions are appreciated. Once again I am a total novice In the programming/scripting world so please have some patience. Thank you. (I wouldn't be opposed to someone posting their controller in the responses and I can compare that to mine.)

Here is my controller so far. (hasn't been working.) using UnityEngine; using System.Collections;

public class PlayerController : MonoBehaviour {

 public float jumpHeight;
 public float moveSpeed;

 //use this for initialization
 void Start () {

 }

 //update is called once per frame
 void Update () {
     if (Input.GetKeyDown (KeyCode.Space)) {
         GetComponent<Rigidbody2D> ().velocity = new Vector2 (GetComponent<Rigidbody2D> (). velocity.x,jumpHeight);
     }

     if (Input.GetKey (KeyCode.D)) {
         GetComponent<Rigidbody2D> ().velocity = new Vector2 (moveSpeed,GetComponent<Rigidbody2D>  ().velocity.y);
     }

     if (Input.GetKey (KeyCode.A)) {
         GetComponent<Rigidbody2D> ().velocity = new Vector2 (-moveSpeed,GetComponent<Rigidbody2D>  ().velocity.y);
     }
 }

}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Guppie1337 · Jun 16, 2015 at 12:18 PM

Unity has some very good examples of how to do this if you watch some of the tutorials they provide.

Space Shooter Tutorial

If you're not interested in following that tutorial, I'll give you a quick rundown of how things workout in the script.

 //You want player movement in FixedUpdate.
 void FixedUpdate ()
 {
   //Create variables to store your values.
   float moveHorizontal = Input.GetAxis ("Horizontal");
   float moveVertical = Input.GetAxis ("Vertical");
 
   //Your movement vector.
   Vector2 movement = new Vector2 (moveHorizontal, moveVertical);
 
   //Move the player.
   GetComponent<Rigidbody2D>().velocity = movement;
 }
 

What's happening here is Input.GetAxis changes values by the left, right, up, down arrow keys. You can use other keywords or even make your own through Edit -> Project Settings -> Input. They are basically Vector2 with coordinates of values from either -1 to 1. e.g (1, 0) = right, (-1, 0) = left, (0, 1) = up, (0, -1) = down.

When FixedUpdate goes through, if you are pressing any of those keys during that frame, it will return one of those values. Your player will then change his velocity for every frame it has a value.

A very common mistake is that people don't quite understand that when you change a rigidbody velocity, you are changing it for good unless you change it again somehow. The reason this code works to stop your character when you want is because when you aren't pressing anything, the value returned is (0, 0), thus bringing your player's velocity to a stop.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How do I stop the player from moving offscreen? 2 Answers

Changing player's moving direction 0 Answers

Please Help - 2D Collisions Without Unity's Physics 1 Answer

My 2D ball keep falling through ground 0 Answers

2D C# destroy a GameObject on collision 2 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