- Home /
,Problems with capsule collider 2d and velocity
Ok this is a really weird bug i'm having for some reason my character (only when moving left to right) will randomly go up and down a bit on the y axis i believe it has something to do with the capsule collider as when i use a box collider its fine it will jut get caught on the groud which is why im not doing that it also happens with a cirle collider heres the code (by the way the comments are old lines of original code a wrote ages ago or stuf a commented out trying to find the bug source): using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Player_Movement : MonoBehaviour { public Rigidbody2D rigidbody2D; public float m_speed; //public float gravity = 3f; public float midaircontrol = 1f; public Player_GC gC; public Bannana_Count bac; public Player_Jump pj; public Player_Health ph; public float display; //rigidbody2D.AddForce(new Vector2(0, -gravity), ForceMode2D.Impulse);
private void Start()
{
rigidbody2D = GetComponent<Rigidbody2D>();
}
//rigidbody2D.velocity = Vector2.right * m_speed;
void Update()
{
//rigidbody2D.AddForce(new Vector2(0, -gravity), ForceMode2D.Impulse);
if (Input.GetKey(KeyCode.A) && Input.GetKey(KeyCode.D) == false)
{
/*if (bac.SpeedChange == false)
{
m_speed = m_speed + (Time.deltaTime * 15f);
if (m_speed >= 30f && bac.SpeedChange == false)
{
m_speed = 30f;
}
}*/
if (gC.Grounded == true)
{
//if (pj.jumping == true)
//{
rigidbody2D.velocity = new Vector2 (-m_speed, rigidbody2D.velocity.y);
display = rigidbody2D.velocity.y;
Debug.Log(rigidbody2D.velocity.y + " A key");
// }
//else if (pj.jumping == false && ph.damagedoneM == false)
//{
// rigidbody2D.velocity = new Vector2(-m_speed, 0);
// }
}
else if(gC.Grounded == false)
{
rigidbody2D.velocity += new Vector2(-m_speed * midaircontrol * Time.deltaTime, 0);
rigidbody2D.velocity = new Vector2(Mathf.Clamp(rigidbody2D.velocity.x, -m_speed, +m_speed), rigidbody2D.velocity.y);
}
}
else
{
if (Input.GetKey(KeyCode.D) && Input.GetKey(KeyCode.A) == false)
{
/*if (bac.SpeedChange == false)
{
m_speed = m_speed + (Time.deltaTime * 15f);
if (m_speed >= 30f && bac.SpeedChange == false)
{
m_speed = 30f;
}
}*/
if (gC.Grounded == true)
{
//if (gC.Grounded == true)
//{
//if (pj.jumping == true)
//{
rigidbody2D.velocity = new Vector2 (m_speed, rigidbody2D.velocity.y);
Debug.Log(rigidbody2D.velocity.y + " D key");
// }
//else if (pj.jumping == false && ph.damagedoneM == false)
//{
//rigidbody2D.velocity = new Vector2(m_speed, 0);
//}
//}
//rigidbody2D.velocity = new Vector2(m_speed, rigidbody2D.velocity.y);
}
else if(gC.Grounded == false)
{
rigidbody2D.velocity += new Vector2(m_speed * midaircontrol * Time.deltaTime, 0f);
rigidbody2D.velocity = new Vector2(Mathf.Clamp(rigidbody2D.velocity.x, -m_speed, +m_speed), rigidbody2D.velocity.y);
}
}
else
{
rigidbody2D.velocity = new Vector2(0f, rigidbody2D.velocity.y);
if (bac.SpeedChange == false)
{
//m_speed = m_speed - (Time.deltaTime * 10);
//if (m_speed <= 15f && bac.SpeedChange == false)
// {
m_speed = 15f;
//}
}
}
}
/*if (Input.GetKeyDown(KeyCode.A) && gC.Grounded == false)
{
//rigidbody2D.AddForce(new Vector2(-m_speed, 0), ForceMode2D.Impulse);
rigidbody2D.velocity = new Vector2(-m_speed, rigidbody2D.velocity.y);
}
if (Input.GetKeyDown(KeyCode.D) && gC.Grounded == false)
{
//rigidbody2D.AddForce(new Vector2(-m_speed, 0), ForceMode2D.Impulse);
rigidbody2D.velocity = new Vector2(m_speed, rigidbody2D.velocity.y);
}
*/
}
}
Your answer