UPDATE: The new script that I combined with my collision script (posted on the very bottom of this page) was giving me all kinds of problems, and by problems I mean things like crashing. So I now have updated the script and made it by itself. I have posted it here on this post. Check it out to see if it will work correctly please when anybody has the time. Thanks in advance. Original post: Here’s a topic that I find interesting seeing how there is so little threads or answers about this. Maybe there are but I can’t find it anywhere. So if anyone has something similar or anything that can help me please post a link or something. Anyways I would like some help with this topic(obviously). My powerup is an invincibility/speed up powerup that kills me after it ends. So for an example I get the powerup, speed up and turn invincible, I stay like this for the amount of time I put it to be. Then after the invincibility and speed up wears off, I slam into an obstacle that I’m supposed to dodge but can’t because I was going so fast. So I need any obstacle that is tagged as dangerous near me to be destroyed. I hope you get what I’m trying to explain and if you don’t, please please tell me. Hope you guys/gals can help. [End] Oh and by the way I am making a 2D game with only 2D assets.
public class DestroyObstacle : MonoBehaviour {
public Camera cam;
void Start (){
}
void Update (){
if (Input.GetMouseButtonDown(1))
{
RaycastHit2D hit = Physics2D.Raycast(cam.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
if(hit != null)
{
Debug.Log("object clicked: "+hit.collider.tag);
}
}
}
}
Thanks for the link. Quick question though, should I try to combine the code with my powerup script, my character script, my collider script, or create an entirely new script?
The powerup script would know when the powerup wears off, but may not know which tags are dangerous, nor does the powerup have the collider where you want the raycast to originate so it would have to get that info. But it is a possible candidate since the new functionality powerup related.
The character script probably has the collider info, probably knows what is dangerous, but may not know when the powerup wears off. But the new functionality doesn’t really fit with the character itself.
If your collider script is what checks if the player is killed, then it knows what is dangerous, may know the collider info and would know when the powerup is done. Its a good candidate since the new functionality is deals with player death and hitting things.
A new script may be a good choice as it could be reused if for example you add a gun/laser/light sword option that destroys things in its path. The character/collider script would tell the new script the raycast info and the danger tags, the powerup can tell it to fire off and for how long.
Okay so I started messing around with raycasts and I came up with this script:
public class Collision : MonoBehaviour {
ControllerScript player;
void Start()
{
player = GetComponent<ControllerScript>();
}
void OnCollisionEnter2D (Collision2D col)
{
// If the colliding gameobject is an Enemy...
if(col.gameObject.tag == "Dangerous")
{
// check if player is NOT invincible
if ( !player.isInvincible ) // this is the same as writing if(player.isInvincible == false)
{
// Find all of the colliders on the gameobject and set them all to be triggers.
Collider2D[] cols = GetComponents<Collider2D>();
foreach(Collider2D c in cols)
{
c.isTrigger = true;
}
// ... disable user Player Control script
GetComponent<ControllerScript>().enabled = false;
renderer.material.color = Color.gray;
}
// else the player IS invincible, destroy all obstacles in the way
else
{
// destroy the Dangerous object
Destroy (col.gameObject); // This is to destroy the obstacles
{
if (Input.GetMouseButtonDown(0)){
Ray ray= Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit = new RaycastHit();
Ray fwd = transform.TransformDirection;
if (Physics.RaycastAll(transform.position, fwd, hit, 60)){ // 1000 or Mathf.Infinity should be enough !
// what did the raycast hit ?
Debug.Log ("Target Hit");
if(hit.collider.gameObject.tag == "Dangerous")
{
Destroy(hit.collider.gameObject);
}
}
}
}
}
}
}
}
}
I actually decided to combine my collision script with the raycast code because I was having trouble getting it to do anything when it was a stand alone script. It has errors in it so I can’t test it in the game until I fix them. Here’s the errors: