- Home /
Question by
mizueon · Nov 18, 2016 at 02:15 PM ·
2draycast2dplatformer
Raycast2D alternatives
hi there! im trying to change block texture based on other blocks around it, i got everything working, but problem is that raycasts are too expensive whenever i test it on actually generated world it lags horribly(consider the fact that im using FixedUpdate and 16 pixel blocks on 1920*1080 screen) here's my code`public Texture2D sprite;
Sprite[] spr;
void Awake(){
//folder should be in resources
spr = Resources.LoadAll <Sprite> ("Sprites/" + sprite.name);
}
void FixedUpdate() {
bool up = Physics2D.Raycast (transform.position + new Vector3 (0f, 0.09f, 0f), Vector2.up, 0.14f);
bool down = Physics2D.Raycast (transform.position + new Vector3 (0f, -0.09f, 0f), Vector2.down, 0.14f);
bool right = Physics2D.Raycast (transform.position + new Vector3 (0.09f, 0f, 0f), Vector2.right, 0.14f);
bool left = Physics2D.Raycast (transform.position + new Vector3 (-0.09f, 0f, 0f), Vector2.left, 0.14f);
if (up == true){
gameObject.GetComponent<SpriteRenderer> ().sprite = spr[0];
}`
and so it continues to complete 16 times. please suggest any performance friendly raycast alternatives or a way to optimise this script... Thanks!
Comment