Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We’re making changes to the Unity Runtime Fee pricing policy that we announced on September 12th. Access our latest thread for more information!
    Dismiss Notice
  3. Dismiss Notice

Bug Bal follow my hand

Discussion in 'VR' started by Programer_YEAA, Jul 5, 2022.

?

How to stop object to follow my controller

  1. You use a wrong method.

    0 vote(s)
    0.0%
  2. You need to do something else.

    1 vote(s)
    100.0%
  1. Programer_YEAA

    Programer_YEAA

    Joined:
    Jun 3, 2022
    Posts:
    91
    Problem: When I hold down my trigger two balls follow my hand.
    What I want: When I hold down my trigger two balls get the same position as my controller. But does not follow it.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.XR;
    5.  
    6. public class Fireball : MonoBehaviour
    7. {
    8.     [SerializeField] private Transform testBoll1;
    9.     [SerializeField] private Transform testBoll2;
    10.  
    11.     [SerializeField] private Transform ballTransform;
    12.     [SerializeField] private Transform leftTransform;
    13.     [SerializeField] private Transform rightTransform;
    14.  
    15.     private List<InputDevice> devices;
    16.  
    17.     private ControllData[] deviceList = new ControllData[2];
    18.  
    19.     void connect() // Place my left and right controller inside a list that I can use later.
    20.     {
    21.         devices = new List<InputDevice>();
    22.         InputDeviceCharacteristics leftController = InputDeviceCharacteristics.Controller | InputDeviceCharacteristics.HeldInHand | InputDeviceCharacteristics.Left;
    23.         InputDevices.GetDevicesWithCharacteristics(leftController, devices);
    24.  
    25.  
    26.         if (devices.Count == 1 && deviceList[0] == null)
    27.         {
    28.             deviceList[0] = new ControllData(leftTransform, devices[0], testBoll1, testBoll2);
    29.         }
    30.  
    31.         devices = new List<InputDevice>();
    32.         InputDeviceCharacteristics rightController = InputDeviceCharacteristics.Controller | InputDeviceCharacteristics.HeldInHand | InputDeviceCharacteristics.Right;
    33.         InputDevices.GetDevicesWithCharacteristics(rightController, devices);
    34.  
    35.         if (devices.Count == 1 && deviceList[1] == null)
    36.         {
    37.             deviceList[1] = new ControllData(rightTransform, devices[0], testBoll1, testBoll2);
    38.         }
    39.  
    40.         devices = new List<InputDevice>();
    41.         InputDeviceCharacteristics controller = InputDeviceCharacteristics.Controller | InputDeviceCharacteristics.HeldInHand;
    42.         InputDevices.GetDevicesWithCharacteristics(controller, devices);
    43.     }
    44.  
    45.     void Update()
    46.     {
    47.         connect();
    48.  
    49.         for (int i = 0; i < devices.Count; i++) // If I press down the trigger two balls get the same position as the controller
    50.         {
    51.             InputDevice targetDevice = devices[i];
    52.  
    53.             targetDevice.TryGetFeatureValue(CommonUsages.triggerButton, out bool buttonValue);
    54.  
    55.             if (buttonValue == false) { continue; }
    56.  
    57.             if ((targetDevice.characteristics.HasFlag(flag: InputDeviceCharacteristics.Left)))
    58.             {
    59.                 deviceList[0].setMoveMarks();
    60.             }
    61.  
    62.             if ((targetDevice.characteristics.HasFlag(flag: InputDeviceCharacteristics.Right)))
    63.             {
    64.                 deviceList[1].setMoveMarks();
    65.             }
    66.         }
    67.     }
    68. }
    69.  
    70. public class ControllData
    71. {
    72.     private Transform controllerTransform;
    73.  
    74.     private Transform[] moveMarks;
    75.  
    76.     [SerializeField] private Transform testBoll1;
    77.     [SerializeField] private Transform testBoll2;
    78.  
    79.     public ControllData(Transform controllerTransform, InputDevice device, Transform testBoll1, Transform testBoll2)
    80.     {
    81.         this.controllerTransform = controllerTransform;
    82.         this.device = device;
    83.  
    84.         moveMarks = new Transform[] { controllerTransform, controllerTransform };
    85.  
    86.         this.testBoll1 = testBoll1;
    87.         this.testBoll2 = testBoll2;
    88.     }
    89.  
    90.     public void setMoveMarks()
    91.     {
    92.         testBoll1.position = moveMarks[0].position;
    93.         testBoll2.position = moveMarks[1].position;
    94.     }
    95. }