- Home /
Help! OnTriggerStay() not working!!!
Help guys! I don't know why, but I have 2 objects in which where if they collide with my cube object the only one that the trigger connects is the capsule one. This capsule is a player in which has player movements for FPS, I tried to copy only the collision part for a cube but it didn't work as intended.
Here's the code for for each one
sphere:
public bool inrange = false;
public void Inrange()
{
inrange = true;
}
public void Outrange()
{
inrange = false;
}
Here's the player:
// Start is called before the first frame update
[SerializeField] Transform PlayerCamera = null;
[SerializeField] float mouseSensitivity = 3.5f;
[SerializeField] bool LockCursor = true;
[SerializeField] float MovementSpeed = 6.0f;
[SerializeField] float Gravity = -13.0f;
[SerializeField] [Range(0.0f, 0.5f)] float MoveSmoothTime = 0.3f;
[SerializeField] [Range(0.0f, 0.5f)] float MouseSmoothTime = 0.03f;
[SerializeField] bool canmove = true;
public bool inrange = false;
float CameraPitch = 0.0f;
float VelocityY = 0.0f;
CharacterController Controller = null;
Vector2 CurrentDirection = Vector2.zero;
Vector2 CurrentDirVelocity = Vector2.zero;
Vector2 CurrentMouseDelta = Vector2.zero;
Vector2 CurrentMouseDeltaVelocity = Vector2.zero;
public void Inrange()
{
inrange = true;
}
public void Outrange()
{
inrange = false;
}
And here's the code for the object of collision:
public void OnTriggerStay (Collider other)
{
this.gameObject.GetComponent<Trigger>().enabled = true;
FindObjectOfType<PlayerController>().Inrange();
FindObjectOfType<batu>().Inrange();
}
public void OnTriggerExit (Collider other)
{
FindObjectOfType<PlayerController>().Outrange();
FindObjectOfType<batu>().Outrange();
}
It's the freaking same! Here's the picture for the object inspection
As you can see in their inspection script, there's a bool for confirming they are in range of the object or not. But the only one that becomes true is the Player not the Sphere
What is colliding with the sphere? Maybe one of the GameObjects that are colliding is missing a RigidBody component.
It's the same GameObject which is a cube and has a Box Collider, but the strange thing is only the Player got any interaction when colliding (OnTriggerStay() or OnTriggerEnter() ) with the cube but the Sphere in which also jave the RigidBody can't
And yes before you ask, I actually tried putting a RigidBody in the Sphere but it still won't connect
Try changing FindObjectOfType<batu>().Inrange();
- and the Outrange() version as well - to GameObject.GetComponent<batu>().Inrange()
- and .Outrange()
So Sphere.getComponent(SphereCode).Inrange() and / or Outrange()?