I downloaded Tower Bridge demo to find out how to create some 2D game basics and I used some scripts etc. But I can’t achieve this effect - in demo, when you jump up and right AND you hit the wall, the player slides down to the ground. However the same doesn’t happen in my game. I use simple 2D box colliders on my walls and my player controller script is following:
public class PlayerScript : MonoBehaviour {
[HideInInspector]
public bool facingRight = true;
public float moveForce = 365f;
public float jumpForce = 200f;
public float maxSpeed = 5f;
private Transform groundCheck;
private bool onGround;
private bool jump;
void Awake() {
groundCheck = transform.Find("groundCheck");
}
void Update() {
onGround = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));
if(Input.GetButtonDown("Jump") onGround) {
jump = true;
}
}
void FixedUpdate() {
float h = Input.GetAxis("Horizontal");
if(h * rigidbody2D.velocity.x < maxSpeed) {
rigidbody2D.AddForce(Vector2.right * h * moveForce);
}
if(Mathf.Abs(rigidbody2D.velocity.x) > maxSpeed) {
rigidbody2D.velocity = new Vector2(Mathf.Sign(rigidbody2D.velocity.x) * maxSpeed, rigidbody2D.velocity.y);
}
if(jump) {
rigidbody2D.AddForce(new Vector2(0, jumpForce));
jump = false;
}
if (h > 0 !facingRight) {
Flip();
} else if (h < 0 facingRight) {
Flip();
}
}
void Flip() {
// Switch the way the player is labelled as facing.
facingRight = !facingRight;
// Multiply the player's x local scale by -1.
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
Yet the player object is sticked to the wall as long as I hold the keys down. Thank you for any advice.