void OnCollisionEnter2D(Collision2D coll) //this portion of the code is what happens when Steve collides with the item // you will have to customize it to act according to the power-up or power-down { if (coll.gameObject.tag == "Player") { StarSound sound = GameObject.FindObjectOfType(typeof(StarSound)) as StarSound; sound.TriggerSound(); SteveScript steve = GameObject.FindObjectOfType <SteveScript>(); steve.tiny(); Destroy(gameObject); } }
public Vector2 OnCollisionEnter2D(Collision2D coll) { if (coll.gameObject.tag == "horizontal") { dir = newDirection(dir.x, -(dir.y)); } if (coll.gameObject.tag == "vertical") { dir = newDirection(-(dir.x), dir.y); } if (coll.gameObject.tag == "bouncer") { MoveScript bouncer1 = gameObject.GetComponent <MoveScript>(); Vector2 position1 = gameObject.transform.position; Vector2 position2 = coll.gameObject.transform.position; Vector2 normal = (position2 - position1); // normal of collision plane (not normalised). float nn = Vector2.Dot(normal, normal); // square length of normal of collision float vn = Vector2.Dot(normal, bouncer1.dir); // impact velocity (collision impulse). bouncer1.dir -= (2.0f * (vn / nn)) * normal; // reflect along the collision plane. dir = bouncer1.dir; } if (coll.gameObject.tag == "Player") { MoveScript bouncer1 = gameObject.GetComponent <MoveScript>(); Vector2 pos1 = gameObject.transform.position; Vector2 pos2 = coll.gameObject.transform.position; Vector2 normal = (pos2 - pos1); // normal of collision plane (not normalised). float nn = Vector2.Dot(normal, normal); // square length of normal of collision float vn = Vector2.Dot(normal, bouncer1.dir); // impact velocity (collision impulse). Vector2 dir1 = dir; GameObject player = GameObject.Find("Steve_Square"); SteveScript steve = player.GetComponent <SteveScript>(); if (steve.dir.x > 0f) { steve.dir.x = 0.75f; } if (steve.dir.x < 0f) { steve.dir.x = -0.75f; } if (steve.dir.y > 0f) { steve.dir.y = 0.75f; } if (steve.dir.y < 0f) { steve.dir.y = -0.75f; } if (((dir.x > 0 && steve.dir.x > 0) && ((dir.y < 0 && steve.dir.y < 0) || (dir.y > 0 && steve.dir.y > 0))) && (pos1.x > pos2.x)) { return(bouncer1.dir = (2.0f * (vn / nn)) * normal); // reflect along the collision plane. } if (((dir.x < 0 && steve.dir.x < 0) && ((dir.y < 0 && steve.dir.y < 0) || (dir.y > 0 && steve.dir.y > 0))) && (pos1.x < pos2.x)) { return(bouncer1.dir = (2.0f * (vn / nn)) * normal); // reflect along the collision plane. } if (((dir.y > 0 && steve.dir.y > 0) && ((dir.x < 0 && steve.dir.x < 0) || (dir.x > 0 && steve.dir.x > 0))) && (pos1.y < pos2.y)) { return(bouncer1.dir = (2.0f * (vn / nn)) * normal); // reflect along the collision plane. } if (((dir.y < 0 && steve.dir.y < 0) && ((dir.x < 0 && steve.dir.x < 0) || (dir.x > 0 && steve.dir.x > 0))) && (pos1.y > pos2.y)) { return(bouncer1.dir = (2.0f * (vn / nn)) * normal); // reflect along the collision plane. } else { return(bouncer1.dir -= (2.0f * (vn / nn)) * normal); // reflect along the collision plane. } } return(dir); }