- Home /
Question by
Gsnoes · Jan 12 at 05:22 PM ·
rigidbody2dplatformerplayer movementrigidbody.velocityspeed issues
Player moves differently in different aspect ratios
When I play in different aspect ratios and when I build my game the player moves drastically different speeds despite the fact that my speed variable stays consistent, my hypothesis is that it has something to do with the update function and different fps's.
This is my current player movement script
public Transform tform;
public SpriteRenderer sprite;
public Rigidbody2D rb;
public float speed = 10;
public float dashSpeed = 10;
public float jumpForce = 10;
public Vector3 spawn_Position;
public bool touchingG = false;
public bool touchingW = false;
private void Update()
{
if (Input.GetKey(KeyCode.A))
{
rb.position = rb.position + new Vector2(-speed, 0f) / 20;
sprite.flipX = true;
}
if (Input.GetKey(KeyCode.D))
{
rb.position = rb.position + new Vector2(speed, 0f) / 20;
sprite.flipX = false;
}
if (Input.GetKey(KeyCode.Space))
{
//Ground Check to jump
if (touchingG == true)
{
rb.velocity = new Vector3(0f, jumpForce, 0);
}
//Wall Check to wall jump
if (touchingW == true)
{
rb.velocity = new Vector3(0f, jumpForce, 0f);
}
}
else
{
//stops player if they lift space key
if (touchingG == true)
{
rb.velocity = new Vector3(0f, 0f, 0f);
}
if (touchingW == true)
{
rb.velocity = new Vector3(0f, -3f, 0f);
}
}
if (Input.GetKeyDown(KeyCode.J))
{
dash();
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
//Ground Check / Wall Check
if (collision.gameObject.tag == "Ground")
{
touchingG = true;
}
if (collision.gameObject.tag == "Wall")
{
touchingW = true;
}
if (collision.gameObject.tag == "Death")
{
tform.position = spawn_Position;
}
}
private void OnCollisionExit2D(Collision2D collision)
{
if (collision.gameObject.tag == "Ground")
{
touchingG = false;
}
if (collision.gameObject.tag == "Wall")
{
touchingW = false; ;
}
}
private void dash()
{
//jank code to cause a sudden increase in rigidbody velocity for dash ability
if (Input.GetKeyDown(KeyCode.J))
{
if (touchingG == false)
{
rb.velocity = new Vector3(dashSpeed, 0f, 0f);
if (sprite.flipX == true)
{
rb.velocity = new Vector3(-dashSpeed, 0f, 0f);
}
}
}
}
Comment