- Home /
When my project is built my polygon collider does not change.
In the editor everything works perfectly, including the faulty PG collier. It goes to ducking mode when you Duck, and normal mode when you are not. When it is built however, it only ever stays in its normal mode. If it helps here is my code:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class Player_Controls : MonoBehaviour {
public float speed, Jump, SpeedIncreaseIncriments, DuckingTime;
public GameObject Death_TapToRestart, Death_Quit, Death_Pick;
[HideInInspector]
public bool canMove = true, canJump = false;
//Ducking is for Phones
bool CanDuck = true, Ducking = false;
float ypos;
Animator anim;
PolygonCollider2D PG;
[HideInInspector]
public Vector2[] WalkingPoints, DuckingPoints;
[HideInInspector]
public int Option;
float DuckingTIME;
void Start() {
DuckingTIME = DuckingTime;
PG = GetComponent<PolygonCollider2D>();
anim = GetComponent<Animator>();
canMove = true;
ypos = transform.position.y;
}
//Changes Speed
void Speed() {
if (Mathf.Round(transform.position.x % 100) == 0) {
speed += SpeedIncreaseIncriments;
}
}
//Sees if you are dead.
void OnCollisionEnter2D(Collision2D col) {
if (col.gameObject.layer == 9) {
canMove = false;
}
}
void Update() {
if (canMove) {
//Are you on the ground?
if (transform.position.y <= ypos) {
//yes
canJump = true;
} else {
//no
canJump = false;
}
//Falling through the World?
if (transform.position.y <= -5) {
canMove = false;
}
// #if UNITY_EDITOR || UNITY_STANDALONE || UNITY_WEBPLAYER
//Key Binded!
DUCK();
//Key Binded!
JUMP();
//#else
//Swipe Controls
if (Swiping()== "Jump") {
if (canJump) {
GetComponent<Rigidbody2D>().AddForce(new Vector2(0, Jump));
}
}
if (Swiping()=="Drag") {
Ducking = true;
}
if(Swiping() == "unDuck") {
Ducking = false;
}
if (Ducking) {
PG.SetPath(0, DuckingPoints);
anim.SetBool("Ducking", true);
DuckingTime -= Time.deltaTime;
if (DuckingTime <= 0) {
DuckingTime = DuckingTIME;
anim.SetBool("Ducking", false);
PG.SetPath(0, WalkingPoints);
Ducking = false;
}
}
// #endif
//UI STuff
Death_TapToRestart.SetActive(false);
Death_Quit.SetActive(false);
Death_Pick.SetActive(false);
//End UI Stuff
anim.SetBool("Dead", false);
Camera.main.transform.Translate(new Vector3(speed * Time.deltaTime, 0, 0));
transform.Translate(new Vector3(speed * Time.deltaTime, 0, 0));
} else {
//UI
Death_TapToRestart.SetActive(true);
Death_Quit.SetActive(true);
Death_Pick.SetActive(true);
//End UI Stuff
anim.SetBool("Dead", true);
// GameObject.Find("World Generator").GetComponent<World_Generation>().Restart();
}
}
private Vector2 touchOrigin = -Vector2.one;
string Swiping() {
float y = 0;
if (Input.touchCount > 0) {
Touch myTouch = Input.touches[0];
if (myTouch.phase == TouchPhase.Began) {
touchOrigin = myTouch.position;
} else if (myTouch.phase == TouchPhase.Ended && touchOrigin.x >= 0) {
Vector2 TouchEnd = myTouch.position;
y = TouchEnd.y - touchOrigin.y;
if (y <= -10f) {
return "Drag";
}
if (y >= 10f) {
return "Jump";
}
return "Jump";
}
}
return null;
}
void JUMP() {
if (canJump) {
if (Input.GetButton("Jump")) {
GetComponent<Rigidbody2D>().AddForce(new Vector2(0, Jump));
}
}
}
//Keybinded Duck
void Duck() {
if (Input.GetButton("Duck")) {
anim.SetBool("Ducking", true);
PG.SetPath(0, DuckingPoints);
}
if (Input.GetButtonUp("Duck")) {
anim.SetBool("Ducking", false);
PG.SetPath(0, WalkingPoints);
}
}
void DUCK() {
if (CanDuck) {
Duck();
}
}
}
Answer by SamuelJZito · Dec 12, 2016 at 02:32 PM
I have fixed it myself thanks to this link: https://www.reddit.com/r/Unity2D/comments/2ge8h0/altering_a_polygon_collider_at_runtime/ and this comment: IMPORTANT: If you want to modify Polygon Colliders based on the shape of a sprite at runtime make sure you set the sprite to "Advanced" and check off "Readable". If you don't do this the collider will work in the editor but not in any builds.
I learned this during the last hour of a game jam when all my colliders broke in the web build...
Your answer
Follow this Question
Related Questions
Unity freezing when trying to build to android? 0 Answers
Unity won't export for android 0 Answers
I have a build problem android. 0 Answers
Unauthorized access exeption when building to Oculus quest 2 0 Answers
build-error 0 Answers