New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Graboverride on Gunscript overrides grab button permanently for any object. #1535

Closed
joschi27 opened this Issue Oct 2, 2017 · 3 comments

Comments

2 participants
@joschi27

joschi27 commented Oct 2, 2017

Expected behavior

Grab the gun with normal grab button, in my case "Trigger Press"
Shoot the gun with Trigger Press (Interaction button)
Drop the gun with the grip button

Current behavior

Grab the gun (My case with grip button)
Drop the gun with grip override button

Grip override button gets "written" onto my controller. I can only grab objects after first interaction with gun with the grip override button configured on the gun.

Gunscript;

namespace VRTK.Examples
{
using UnityEngine;

public class RealGun : VRTK_InteractableObject
{
    public float bulletSpeed = 200f;
    public float bulletLife = 5f;

    private GameObject bullet;
    private GameObject trigger;
    private RealGun_Slide slide;
    private RealGun_SafetySwitch safetySwitch;

    private Rigidbody slideRigidbody;
    private Collider slideCollider;
    private Rigidbody safetySwitchRigidbody;
    private Collider safetySwitchCollider;

    private VRTK_ControllerEvents controllerEvents;

    private float minTriggerRotation = -10f;
    private float maxTriggerRotation = 45f;

    private void ToggleCollision(Rigidbody objRB, Collider objCol, bool state)
    {
        objRB.isKinematic = state;
        objCol.isTrigger = state;
    }

    private void ToggleSlide(bool state)
    {
        if (!state)
        {
            slide.ForceStopInteracting();
        }
        slide.enabled = state;
        slide.isGrabbable = state;
        ToggleCollision(slideRigidbody, slideCollider, state);
    }

    private void ToggleSafetySwitch(bool state)
    {
        if (!state)
        {
            safetySwitch.ForceStopInteracting();
        }
        ToggleCollision(safetySwitchRigidbody, safetySwitchCollider, state);
    }

    public override void Grabbed(VRTK_InteractGrab currentGrabbingObject)
    {
        base.Grabbed(currentGrabbingObject);

        controllerEvents = currentGrabbingObject.GetComponent<VRTK_ControllerEvents>();

        ToggleSlide(true);
        ToggleSafetySwitch(true);

        //Limit hands grabbing when picked up
        if (VRTK_DeviceFinder.GetControllerHand(currentGrabbingObject.controllerEvents.gameObject) == SDK_BaseController.ControllerHand.Left)
        {
            allowedTouchControllers = AllowedController.LeftOnly;
            allowedUseControllers = AllowedController.LeftOnly;
            slide.allowedGrabControllers = AllowedController.RightOnly;
            safetySwitch.allowedGrabControllers = AllowedController.RightOnly;
        }
        else if (VRTK_DeviceFinder.GetControllerHand(currentGrabbingObject.controllerEvents.gameObject) == SDK_BaseController.ControllerHand.Right)
        {
            allowedTouchControllers = AllowedController.RightOnly;
            allowedUseControllers = AllowedController.RightOnly;
            slide.allowedGrabControllers = AllowedController.LeftOnly;
            safetySwitch.allowedGrabControllers = AllowedController.LeftOnly;
        }
    }

    public override void Ungrabbed(VRTK_InteractGrab previousGrabbingObject)
    {
        base.Ungrabbed(previousGrabbingObject);

        ToggleSlide(false);
        ToggleSafetySwitch(false);

        //Unlimit hands
        allowedTouchControllers = AllowedController.Both;
        allowedUseControllers = AllowedController.Both;
        slide.allowedGrabControllers = AllowedController.Both;
        safetySwitch.allowedGrabControllers = AllowedController.Both;

        controllerEvents = null;
    }

    public override void StartUsing(VRTK_InteractUse currentUsingObject)
    {
        base.StartUsing(currentUsingObject);
        if (safetySwitch.safetyOff)
        {
            slide.Fire();
            FireBullet();
            VRTK_ControllerHaptics.TriggerHapticPulse(VRTK_ControllerReference.GetControllerReference(controllerEvents.gameObject), 0.63f, 0.2f, 0.01f);
        }
        else
        {
            VRTK_ControllerHaptics.TriggerHapticPulse(VRTK_ControllerReference.GetControllerReference(controllerEvents.gameObject), 0.08f, 0.1f, 0.01f);
        }
    }

    protected override void Awake()
    {
        base.Awake();
        bullet = transform.Find("Bullet").gameObject;
        bullet.SetActive(false);

        trigger = transform.Find("TriggerHolder").gameObject;

        slide = transform.Find("Slide").GetComponent<RealGun_Slide>();
        slideRigidbody = slide.GetComponent<Rigidbody>();
        slideCollider = slide.GetComponent<Collider>();

        safetySwitch = transform.Find("SafetySwitch").GetComponent<RealGun_SafetySwitch>();
        safetySwitchRigidbody = safetySwitch.GetComponent<Rigidbody>();
        safetySwitchCollider = safetySwitch.GetComponent<Collider>();
    }

    protected override void Update()
    {
        base.Update();
        if (controllerEvents)
        {
            var pressure = (maxTriggerRotation * controllerEvents.GetTriggerAxis()) - minTriggerRotation;
            trigger.transform.localEulerAngles = new Vector3(0f, pressure, 0f);
        }
        else
        {
            trigger.transform.localEulerAngles = new Vector3(0f, minTriggerRotation, 0f);
        }
    }

    private void FireBullet()
    {
        GameObject bulletClone = Instantiate(bullet, bullet.transform.position, bullet.transform.rotation) as GameObject;
        bulletClone.SetActive(true);
        Rigidbody rb = bulletClone.GetComponent<Rigidbody>();
        rb.AddForce(bullet.transform.forward * bulletSpeed);
        Destroy(bulletClone, bulletLife);
    }
}

}

-> With grab override button configured as "Grip Press"

I'm in the vrtk chat if you need any more info.

@joschi27

This comment has been minimized.

Show comment
Hide comment
@joschi27

joschi27 Oct 3, 2017

Update; It overrides only the controller that held the gun.

joschi27 commented Oct 3, 2017

Update; It overrides only the controller that held the gun.

@thestonefox

This comment has been minimized.

Show comment
Hide comment
@thestonefox

thestonefox Oct 3, 2017

Owner

You've not provided steps to reproduce the issue in an example scene, just a whole lot of code

Owner

thestonefox commented Oct 3, 2017

You've not provided steps to reproduce the issue in an example scene, just a whole lot of code

@thestonefox

This comment has been minimized.

Show comment
Hide comment
@thestonefox

thestonefox Oct 3, 2017

Owner

PR with fix #1536

Owner

thestonefox commented Oct 3, 2017

PR with fix #1536

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment