- Home /
Question by
Nycrael · Apr 22, 2015 at 03:29 PM ·
c#2djumpplatformer
Player stucken in the wall while jumping 2D
Hello. Player stucked in wall while jumping (when i try to move him), how to let him fall?
[SerializeField] private float m_MaxSpeed = 10f;
[SerializeField] private float m_JumpForce = 400f;
[SerializeField] private bool m_AirControl = false;
[SerializeField] private LayerMask m_WhatIsGround;
private Transform m_GroundCheck;
const float k_GroundedRadius = .2f;
private bool m_Grounded;
private Animator m_Anim;
private Rigidbody2D m_Rigidbody2D;
private bool m_FacingRight = true;
private void Awake()
{
m_GroundCheck = transform.Find("GroundCheck");
m_Anim = GetComponent<Animator>();
m_Rigidbody2D = GetComponent<Rigidbody2D>();
}
private void FixedUpdate()
{
m_Grounded = false;
Collider2D[] colliders = Physics2D.OverlapCircleAll(m_GroundCheck.position, k_GroundedRadius, m_WhatIsGround);
for (int i = 0; i < colliders.Length; i++)
{
if (colliders[i].gameObject != gameObject)
m_Grounded = true;
}
m_Anim.SetBool("Ground", m_Grounded);
// Set the vertical animation
m_Anim.SetFloat("vSpeed", m_Rigidbody2D.velocity.y);
}
public void Move(float move, bool crouch, bool jump)
{
if (m_Grounded || m_AirControl)
{
m_Anim.SetFloat("Speed", Mathf.Abs(move));
m_Rigidbody2D.velocity = new Vector2(move*m_MaxSpeed, m_Rigidbody2D.velocity.y);
if (move > 0 && !m_FacingRight)
{
Flip();
}
else if (move < 0 && m_FacingRight)
{
// ... flip the player.
Flip();
}
}
if (m_Grounded && jump && m_Anim.GetBool("Ground"))
{
m_Grounded = false;
m_Anim.SetBool("Ground", false);
m_Rigidbody2D.AddForce(new Vector2(0f, m_JumpForce));
}
}
private void Flip()
{
// Switch the way the player is labelled as facing.
m_FacingRight = !m_FacingRight;
// Multiply the player's x local scale by -1.
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
truble.png
(105.7 kB)
Comment
Answer by acorrow · Apr 22, 2015 at 07:58 PM
You might want to look into Physics Materials:
http://docs.unity3d.com/Manual/class-PhysicsMaterial2D.html
Specifically, you will see in the Standard Assets pack there is a "Slippery" material (along with Bouncy and Sticky).
There are some good tutorials on it out there. ALSO!!!! As soon as you figure out how to use physics materials, you will want to look up EFFECTORS (and specifically the PLATFORM effector). Just sayin...
Hope that helps
Adam
Your answer
Follow this Question
Related Questions
GetButtonDown not always firing 1 Answer
Multiple Cars not working 1 Answer
How to make character stop at wall? 1 Answer
Why my variable istn't always used? 2 Answers