OnTriggerStay Doesn't work if not moving
Hello,
if I instantiate a sphere with a trigger collider "inside" of a wall, it doesn't happen what is supposed to happen.
If I instantiate the sphere outside of the wall and move it into the wall, it works.
Why, and how can I solve it?
Answer by TonyLi · Jun 21, 2013 at 01:46 PM
Review all the conditions on the Rigidbody Sleeping page. Are your objects set up correctly with rigidbodies or static colliders? Given your description, the sphere should probably be a static collider marked Is Trigger, and the sphere should have a rigidbody.
If you instantiate one collider inside another, it will not register OnTriggerEnter messages, but it should register OnTriggerStay.
The sphere is instantiated from the $$anonymous$$ainCharacter, as a way to make an attack. It Instantiates in front of him.
If I get clse to the wall and hit the attack button, the sphere would be instantiated inside the wall, but nothing will happen despite the "OnTriggerStay" function it has...
Please post screenshots of the fully-expanded Inspector view for the sphere and the wall.
Those game objects look good to me. Can you post your Js_Attacks script? I suspect that you need to use OnTriggerStay ins$$anonymous$$d of OnCollisionStay. I'll suggest that as an answer.
Here's a unitypackage that demonstrates a combination that works: https://dl.dropboxusercontent.com/u/7669211/BallWallCollision.unitypackage
Import it and open the scene in the Test folder. When you press play, you can move around with the first person controller. The X key spawns and destroys the ball. The ball prints collisions in the console.
It's on my public dropbox, so no guarantees that it'll be there forever. But if someone's reading this in the future and it's gone, the settings and code are in the previous comments. Here's the ball-spawning code:
public class SpawnBall : $$anonymous$$onoBehaviour {
public GameObject ballPrefab;
private GameObject ball = null;
void Update() {
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.X)) {
if (ball == null) {
ball = Instantiate(ballPrefab, Vector3.zero, Quaternion.identity) as GameObject;
ball.transform.parent = this.transform;
ball.transform.localPosition = new Vector3(0f ,1f, 1f);
} else {
Destroy(ball);
ball = null;
}
}
}
}
Answer by YagoRG · May 01, 2021 at 03:42 PM
I know I am way too late for this... but just in case anyone stumbles upon the same problem here's what worked for me.
I solved it by calling the rigidbody and applying a foce of zero to it inside the void Update, that way it won't go to sleep.
In my case was for a 2D game so it ended up looking like this:
Rigidbody2D rb;
// Update is called once per frame
void Update()
{
rb.AddForce(Vector2.zero);
}
I bet there's better ways to do this but this one is really simple and works for a fast and easy fix.
Lifesaver! Thank you for posting late, you saved me so much time and struggle!
Your answer
Follow this Question
Related Questions
Destroy instatiate object on trigger enter / collision,destroy instantiate prefab on trigger enter 0 Answers
My code is instantiating many prefabs, i only want one. 1 Answer
How do I differentiate between the triggers? 1 Answer
TIme -Bomb exploding issue 0 Answers
OnTriggerEnter2D not working 2 Answers