Author Topic: Looking for someone to help refactor/bug fix c# unity code  (Read 1439 times)

im working on a project and i cant for the life of me get some of the stuff to work. I'm extremely frustrated and ready to throw this project in the bin because little glitches or malfunctions cause stuff to break and i wanna throw my computer against a wall god damnit i hate lasagna. it involves pixel perfect physics and it's a platformer game btw.

neeway message me on steam if interested ayy: http://steamcommunity.com/id/bieme/

At least tell me what the bug is first please. :)

At least tell me what the bug is first please. :)



here's a gif, movement horizontal works fine but when i touch these leaf platforms i seem to stick to them for a bit before being pushed out. it's really annoying. also sometimes when i land, I go into the floor for a second and then i am pushed back out.. hmm

I have a GameObject of player with the playerController, Box Collider2D, and Rigidbody2D on it.. here's my code

Code: [Select]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour {

private Rigidbody2D body;
public float speed = 10f;
public float jumpPower = 5000f;
private bool grounded;
private Vector2 groundCheckPos;

public int hp = 3;

public bool canbedamaged = true;

Animator animator;

SpriteRenderer spriterenderer;

public GameObject weapon;

SpriteRenderer weapon_spr;

// Use this for initialization
void Start () {
body = GetComponent<Rigidbody2D> ();

spriterenderer = GetComponentInChildren<SpriteRenderer> ();
;
}

public bool isGrounded(){
bool result =  Physics2D.Linecast(new Vector2(transform.position.x + 8f, transform.position.y - 8f), groundCheckPos , 1 << LayerMask.NameToLayer("Ground"));
if (result) {
Debug.DrawLine(new Vector2(transform.position.x + 8f, transform.position.y - 8f) , groundCheckPos , Color.green, 0.5f, false);
}
else {
Debug.DrawLine(new Vector2(transform.position.x + 8f, transform.position.y - 8f) , groundCheckPos , Color.blue, 0.5f, false);
}
return result;
}

// Update is called once per frame
void FixedUpdate () {
float h = Input.GetAxisRaw ("Horizontal");


if (Mathf.Sign (h) == -1) {
spriterenderer.flipX = true;
} else if (h == 0) {

} else {
spriterenderer.flipX = false;

}



transform.position = new Vector2(transform.position.x + (speed * h * Time.deltaTime), transform.position.y);

float v = Input.GetAxisRaw ("Vertical");

groundCheckPos = new Vector2 (transform.position.x + 8f, transform.position.y - 16.1f);

if (isGrounded ()) {
body.AddForce ((Vector2.up * jumpPower) * v);
} else {

}

if (Input.GetKeyDown (KeyCode.G)) {
damage (-1);
}

while (Physics2D.Linecast (transform.position, new Vector2 (transform.position.x, transform.position.y + 1f), 1 << LayerMask.NameToLayer ("Ground"))) {
transform.position = new Vector2(transform.position.x, transform.position.y - 1f);
}

while (Physics2D.Linecast (transform.position, new Vector2 (transform.position.x - ((1f / 16f) * 3f), transform.position.y), 1 << LayerMask.NameToLayer ("Ground"))) {
transform.position = new Vector2(transform.position.x + (1f / 16f), transform.position.y);
}

while (Physics2D.Linecast (transform.position, new Vector2 (transform.position.x + ((1f / 16f) * 3f), transform.position.y), 1 << LayerMask.NameToLayer ("Ground"))) {
transform.position = new Vector2(transform.position.x - (1f / 16f), transform.position.y);
}

if (spriterenderer.material.GetFloat ("_FlashAmount") > 0) {
spriterenderer.material.SetFloat ("_FlashAmount", spriterenderer.material.GetFloat ("_FlashAmount") - 0.02f);
} else {
canbedamaged = true;
}


}

void Update() {

}

void damage(float direction) {
body.AddForce (Vector2.up * 300);
body.AddForce ((Vector2.left * Mathf.Sign (-direction)) * 100);
canbedamaged = false;
spriterenderer.material.SetFloat ("_FlashAmount", 1f);
}

void OnCollisionEnter2D(Collision2D coll) {
if ((coll.gameObject.tag == "Enemy") && (canbedamaged)) {
damage (Mathf.Sign(transform.position.x - coll.gameObject.transform.position.x));
}

}

}

there's a lot of stuff in there

Your bug is you have the box vertical movement queued to change to static values at set points in time after jumping. You need to apply gravity as a delta to the box's velocity at each point in time and have hitting a ceiling cancel out vertical velocity

Edit: you may actually just have forgotten to clear the box's vertical velocity on ceiling object hit, since your landing on platforms is fine and doesnt phase thru the ground
« Last Edit: August 13, 2017, 05:57:43 PM by Conan »

Your bug is you have the box vertical movement queued to change to static values at set points in time after jumping. You need to apply gravity as a delta to the box's velocity at each point in time and have hitting a ceiling cancel out vertical velocity

Edit: you may actually just have forgotten to clear the box's vertical velocity on ceiling object hit, since your landing on platforms is fine and doesnt phase thru the ground

the first while statement should automatically push you down though. it should complete between the frames

the first while statement should automatically push you down though. it should complete between the frames
pushing your position down doesnt change your velocity, likely.

its definitely an issue with gravity not being applied correctly though

I'll look at this later tonight if you haven't fixed it by then.