- Home /
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 :
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...:
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.
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Jump Code Not Working (2D C#) 2 Answers
Movement Script Only Works in One Scene 2 Answers
Multiple Cars not working 1 Answer
How do I stop the player from gliding while in the air? 1 Answer