Having a couple of problems regarding 2D Movement.
Hello there. I'm quite new to coding so these are some nooby questions. I am making a 2D Platformer and I have the movement setup but I have a couple of things that annoy me.
When you are moving and then you take your finger off the key, instead of stopping completely and immediately, it slides for a second or so before stopping. I am not 100% sure how to make it just instantly stop.
The player can stick to walls, meaning it can pretty much climb walls. I want it to just fall when it hits a wall, but I am not 100% sure how to do that.
Here is my code:
public float jumpHeight; public float moveSpeed;
public Transform groundCheck;
public float groundCheckRadius;
public LayerMask whatIsGround;
private bool grounded;
private bool doubleJumped;
// Use this for initialization
void Start () {
}
void FixedUpdate(){
grounded = Physics2D.OverlapCircle (groundCheck.position, groundCheckRadius, whatIsGround);
}
// Update is called once per frame
void Update () {
if (grounded) {
doubleJumped = false;
}
if (Input.GetKeyDown (KeyCode.W)&& grounded ) {
GetComponent<Rigidbody2D>().velocity = new Vector2 (GetComponent<Rigidbody2D>().velocity.x, jumpHeight);
Jump ();
}
if (Input.GetKeyDown (KeyCode.W) && !doubleJumped && !grounded ) {
GetComponent<Rigidbody2D>().velocity = new Vector2 (GetComponent<Rigidbody2D>().velocity.x, jumpHeight);
Jump ();
doubleJumped = true;
}
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); }
} public void Jump() { GetComponent ().velocity = new Vector2 (GetComponent ().velocity.x, jumpHeight); } }
Answer by Cam Edwards · Sep 05, 2015 at 10:53 PM
I'm making a similar game at the moment, and have just come across both of these problems.
Your first problem is relatively easy to solve. You are already setting the velocity of the GameObject's Rigidbody if the player is pressing the A or D keys, so all you need to do to stop instantly is to restructure your existing movement code a bit, to look like this:
if (Input.GetKey (KeyCode.D))
{
// Your code to move left
}
else if (Input.GetKey (KeyCode.A))
{
// Your code to move right
}
else
{
GetComponent<Rigidbody2D>().velocity = new Vector2 (0, GetComponent<Rigidbody2>().velocity.y);
}
This means that if you are pressing A, you move left, or if you are pressing D you move right, but if you press neither, your velocity in the X direction is set to nothing.
It's worth pointing out that you are calling GetComponent() a lot in your code. You probably wont notice any performance loss by doing this a few times, but if you are constantly searching for components you might find your game slowing down. A better way to do this is to create a variable to hold your rigid body, and only search for it once.
private RigidBody2D body;
void Start()
{
body = GetComponent<Ridigbody2D>();
}
void Update()
{
...
body.velocity = new Vector2(...)
...
}
For your second question, I'm still working on that! I've found that the 2D asset package has a similar character controller to what I'm looking for, you can import it into your own project to have a look. They stop the player sticking to walls by adding an extra BoxCollider2D to edges of platforms, and give it a physics material with 0 friction.
Hm. After I added the line : else { body.velocity = new Vector2 (-0, body.velocity.y); } my "D" key did not want to do anything, and it said that there were no errors in the code. Thank you though. I'm going to try and fix this, I am not sure why it is like this.
It sounds like regardless of whether you press D or not, velocity.x is set to 0. Check your if statement, note that I changed your
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.A))
line to read
else if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.A))
If that isn't the problem, I think I'd need to see all of your code to work out what's wrong,
@Cam Edwards I didn't notice that, that was the actual problem. Thanks again for helping, I really appreciate it.
Edit: I've also found out how to stop my character from sticking to walls. Create a new Physics2D $$anonymous$$aterial and then change the friction to 0. After that just assign it to your box collider(s).
Well, that's a great point! I had tried that originally before I added the code to stop instantly, but I started skidding all over the place as if I was on ice. That's not a problem if you stop your character yourself. Thanks for fixing that problem for me!