Player cannot destroy enemy
I have a game in which my main character "dashes" which triples their movement speed and allows them to destroy enemies. Player Code: //Starting Dash Coroutine.
if (Input.GetButtonDown("Fire1"))
{
if (timeStamp <= Time.time)
{
StartCoroutine(Dash());
}
}
}
public IEnumerator Dash()
{
isdashing = true;
normalspeed = dashspeed;
yield return new WaitForSeconds(dashtime);
normalspeed = speed;
timeStamp = Time.time + coolDownPeriodInSeconds;
isdashing = false;
}
Enemy Code:
public Movement player;
public void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Player" && player.isdashing == true)
{
Destroy(gameObject);
}
}
However, the player does not destroy the enemy. Collision detection works fine, both with other objects AND with the enemy (the player just collides/ bounces off of them. What is the problem with my code?
the code to dash seem to be ok, check if you set the tag player properly, if player variable is correct assigned on script(must be, otherwise should give error), and most probably, check if you have the player (or enemy) with a non kinematic rigidbody, if both have kinematic rigidbodies the OnCollisionEnter
message cant be sent
in the other hand, you script have a bug (can be used to hack a long dash), you dont check if you already are dashing before start coroutine, so, if you press Fire1
again during the dash you start other coroutine, increasing the time to cooldown, and, if you press Fire1
on the exact milisecond that dash end (the old cheat engine or some similar automated system can handle this) you can start other continuos dash without cooldown, solve it just checking if already dashing on the Update
if
if (Input.GetButtonDown("Fire1")&& !isdashing)
{
if (timeStamp <= Time.time)
{
StartCoroutine(Dash());
}
}
Answer by f03n1x · Feb 09, 2019 at 07:28 AM
Looking at the enemy code, I guess you can start off by checking some information using debug print outs, to find the underlying issue
Inside OnCollisionEnter could you do the following:
public void OnCollisionEnter(Collision collision)
{
Debug.Log(collision.gameObject.tag);
Debug.Log(player.isdashing);
if (collision.gameObject.tag == "Player" && player.isdashing == true)
{
Destroy(gameObject);
}
}
You need to check to see if the tag is correct and that the player is dashing, I guess one other question is how are you getting the player gameobject (Movement player)? since that could be the issue.
he post thats collide/bounce, coliders arent overlapping, so cant be player movement using transform, he probably move it using rigidbodies or char controller.
If they collide with each other then it definitely is working, colliders are not supposed to normally overlap, the outer edge of the game object, or at least the collidable area should only touch that of another collidable area, they should only overlap if the colliders have been set to trigger.
I also meant the $$anonymous$$ovement class with the variable name player not the transform for the player at all (It's shown in the enemy class code snippet) I wasn't entirely sure where the information for that $$anonymous$$ovement class was being obtained, it could be possible that the variable is not being updated and returns null which might mean that the bool check player.isdashing remains false regardless of if it's true.
sorry, I believed to "movement" you was refering the way he moves the player, and if you move it using transform, colliders can overlap
the "player sript" that he put a piece of code first is the $$anonymous$$ovement
class that are you asking
if you see my first comment (6 hours before your answer) you see
if player variable is correct assigned on script(must be, otherwise should give error)
the error I was refering is a nullReferenceExeption
to call a field player.isdashing
without initialize player
, don't gives false, this case is only possible if you just call player
, since he don't mention any error, player
must be assigned, the problem is he can have more than one $$anonymous$$ovement script on scene and (maybe) had assigned the wrong one there? idk, lets see what he say.
Your answer
Follow this Question
Related Questions
Cube wont destroy on collision 0 Answers
Lose Health when GameObject enters collider? (OnTriggerEnter) 2 Answers
[SOLVED]OverlapingSphere damage only one enemy 1 Answer
Collision With Text 0 Answers