• 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 FoXGliTcH · May 24, 2015 at 02:59 PM · c#movementscene2d-platformer

2D Character Movement

Hello guys, 2 days ago I started making 2d platformer, but I'm having some troubles with character movement, I've set the movement the script, box collider 2d, rigidbody 2d to the player (character) also box collider 2d to the ground. When I click play, the character does the idle animation, then I press D button or A and it does the walk animation,but it doesn't move. Here's my script: using UnityEngine; using System.Collections;

 public class PlayerKiller
 : MonoBehaviour {
     
     public float speed = 50f;
     public float jumpPower = 150f;
     public bool grounded;
     private Rigidbody2D rb2d;
 
     void Start () 
     {
         rb2d = gameObject.GetComponent<Rigidbody2D> ();
 
     }
 
     void Update () 
     {
         float h = Input.GetAxis("Horizontal");
         rb2d.AddForce ((Vector2.right * speed) * h);
     }
 
 }

Well this is the inspector of the character : alt text

playerproblem.png (49.5 kB)
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
0

Answer by DoTA_KAMIKADzE · May 24, 2015 at 08:52 PM

I can't see any problem in your setup, unless you've made something strange with your ground or "tweaked" your Horizontal axis, it should have a and d and sensitivity and...:

alt text

Also have you tried to check if your Axis returns something? ~like that:

     float h = Input.GetAxis("Horizontal");
     Debug.Log(h);
     rb2d.AddForce((Vector2.right * speed) * h);
     Debug.Log("final value: " + ((Vector2.right * speed) * h).ToString());

In your console you should see some value and your "final value" should return some other value on X and 0.0 on Y.

P.S. If values will be something else than zero then most likely there is something with your animation or ground (e.g. you're not setting your character position to some point in space with animation, are you?).

P.P.S. Also some unrelated to your question info - just want to make sure that you know about velocity and you're consciously using AddForce "skyrocketing" your player.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

Multiple Cars not working 1 Answer

Jump Code Not Working (2D C#) 2 Answers

Distribute terrain in zones 3 Answers

How do I stop the player from gliding while in the air? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges