- Home /
Object bounces off wall, but on return goes through it
I'm playing a game where enemies are coming at your base and you can build walls to hold them off. If an enemy hits your wall, they bounce back. Then they try to go after the wall again until it's destroyed. The first bounceback works fine. Later bounces go straight through the wall. The wall has a collider on it, and so does the enemy. The enemy also has a rigidbody on them. My knockback function is:
void Update()
{
if (noWall)
rigid.MovePosition(Vector3.MoveTowards(transform.position, SelectedTarget.position, speed * Time.deltaTime));
if (!noWall)
{
StartCoroutine(KnockBack());
}
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Wall")
{
noWall = false;
}
}
IEnumerator KnockBack() //causes enemy to be propelled backwards, momentarily stop to end the backwards force, then move forwards again
{
Vector3 dir = SelectedTarget.position - transform.position;
dir = -dir.normalized;
rigid.AddForce(dir * 10);
yield return new WaitForSeconds(1f);
rigid.velocity = new Vector3(0, 0, 0);
rigid.MovePosition(Vector3.MoveTowards(transform.position, SelectedTarget.position, speed * Time.deltaTime)); //this movement goes through my walls
noWall = true;
}
I have two types of enemies, large and small. Large enemies do bounce off indefinitely unless they're knocked forwards. Small enemies bounce one, and then go through. I'm not sure why this is happening. Is there someone who can explain this?
Just a suggestion on your script. You can basically remove rigid.$$anonymous$$ovePosition(Vector3.$$anonymous$$oveTowards(transform.position, SelectedTarget.position, speed * Time.deltaTime)); from your knockback method.
and also just call your $$anonymous$$nockBack method inside the OnTriggerEnter() ins$$anonymous$$d of calling it in the update.
Thanks! I made those changes, and it actually fixed my problem. For reasons that I don't really understand.
Answer by Impstar · Oct 24, 2017 at 06:00 AM
I got it to work, here's the working version. Minor changes and I'm not sure why it matters, but apparently it does.
void Update()
{
if (noWall)
rigid.MovePosition(Vector3.MoveTowards(transform.position, SelectedTarget.position, speed * Time.deltaTime));
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Wall")
{
noWall = false;
StartCoroutine(KnockBack());
}
}
IEnumerator KnockBack() //causes enemy to be propelled backwards, momentarily stop to end the backwards force, then move forwards again
{
Vector3 dir = SelectedTarget.position - transform.position;
dir = -dir.normalized;
rigid.AddForce(dir * 10);
yield return new WaitForSeconds(1f);
rigid.velocity = new Vector3(0, 0, 0);
noWall = true;
}
I moved the StartCoroutine to the OnTriggerEnter function and got rid of the
rigid.MovePosition(Vector3.MoveTowards(transform.position, SelectedTarget.position, speed * Time.deltaTime))
down in the Knockback function.
It's good to know that fixed your problem. Though logically, your original script should work. $$anonymous$$ust be due to Coroutine. I just learned that many are against it. I don't know why but maybe this is one of the reasons.
Your answer
Follow this Question
Related Questions
Similar movement to a game (look description) 1 Answer
How do you make an wire/rope type object and attach to 2 separate objects 1 Answer
How to apply frictional Torque 2 Answers
Physics.overlapSphere doesnt detect colliders,Physics.CheckSphere doesn't detect colliders 1 Answer
Problem is not Bouncing 0 Answers