Home > database >  How to get OnTriggerStay to work on every frame?
How to get OnTriggerStay to work on every frame?

Time:10-14

so I have this code :

public class ball_physics : MonoBehaviour
{
    public Rigidbody ball;

    public open2close claw;
    public Vector3 offset;
    Rigidbody rb;

    private float forceMultiplier = 20;
    private bool isShoot;
    Vector3 start_position;

    public path path;
    
    void Start()
    {
        rb = GetComponent<Rigidbody>();
        rb.Sleep();
        start_position = transform.position;
    }

    void Update()
    {
        // *************
    }

    void Shoot(Vector3 Force)
    {
        if (isShoot)
        {
            print("isshot false");
            return;
        }

        rb.AddForce(new Vector3(Force.x, Force.y,Force.z)* forceMultiplier);
        isShoot = true;
        path.Instance.HideLine();
    }

    private void OnTriggerStay(Collider ball)
    {
        if (isShoot)
        {
            return;
        }
        print("ontriggerstay");


        if (claw.isClosed && claw.transform.gameObject.tag == "claw" )
        {
            print("claw");
            //rb.Sleep();

            transform.position = claw.rightClaw.transform.position   offset;

            Vector3 forceInit = (start_position - transform.position);
            path.Instance.UpdateTrajectory(forceInit * forceMultiplier, rb, transform.position);
        }
    }

    private void OnTriggerExit(Collider ball)
    {
        if (claw.isClosed && claw.transform.gameObject.tag == "claw")
        {
            rb.WakeUp();
        }

        if (claw.isClosed == false)
        {
            Shoot(start_position - transform.position);
        }
    }
}

So on my other codes I have OnStayTrigger, which basically a clawhand grabs a ball and pulls. But this code is suppose to show a trajectory line and shoot. When I shoot the first time it works. But when I try again, The clawhand can grab the ball but it doesnt show the trajectory line nor shoots it. So im guessing this is the code that needs to be fixed. How can I make OnTriggerStay work all the time. I wouldnt want to change any code because it works perfectly. How can I just keep updating the OnTriggerstay so it can work?

CodePudding user response:

At first glance it appears that You never set isShoot back to false, so it would never fire again because in OnTriggerStay you have it return if isShoot = true.

  • Related