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);
}
}
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.
– gro420It 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.GetKey (KeyCode.A)) line to read else if (Input.GetKey (KeyCode.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@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 Material and then change the friction to 0. After that just assign it to your box collider(s).
– gro420Well, 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!
– Cam_EdwardsYou have to remove all these instances vars and be able to target your gameobject specifically. You have multiple ways to do that. You can use GameObject.Find for the quick and ugly way. Youcan also use gameobject references...
– MathieuBarbier