Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Question VR Boat Control Mechanics

Discussion in 'VR' started by wholast710, May 14, 2023.

  1. wholast710

    wholast710

    Joined:
    Feb 11, 2018
    Posts:
    24
    I am trying to make a boat control game with vr. I am using the VRIF Package. I have done the boat, steering and lever operations. When I push the lever forward, the ship moves, but when I turn the steering wheel, a ship does not turn. What could be the reason for this?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using BNG;
    5. using TMPro;
    6. public class ShipControl : MonoBehaviour
    7. {
    8.     public TextMeshPro SpeedText;
    9.  
    10.     Rigidbody rigid;
    11.     public float moveSpeed;
    12.     public float rotationSpeed;
    13.  
    14.     [SerializeField]
    15.     private JoystickVehicleControl _joystickVehicleControl;
    16.  
    17.     [SerializeField]
    18.     private SteeringWheel _steeringWheel;
    19.  
    20.  
    21.     private InputSystemShipInputProvider inputSystemShipInputProvider;
    22.  
    23.     private void Awake()
    24.     {
    25.         inputSystemShipInputProvider = FindObjectOfType<InputSystemShipInputProvider>();
    26.         rigid = GetComponent<Rigidbody>();
    27.     }
    28.  
    29.     private void Update()
    30.     {
    31.         float moveAmount = _joystickVehicleControl.angleX * moveSpeed;
    32.         rigid.AddRelativeForce(Vector3.forward * moveAmount);
    33.  
    34.        
    35.         float steerAngle = _steeringWheel.Angle;
    36.         Vector3 rotation = new Vector3(0f, steerAngle * rotationSpeed, 0f);
    37.         transform.Rotate(rotation * Time.deltaTime);
    38.  
    39.         float rotationAmount = -_steeringWheel.Angle * rotationSpeed;
    40.  
    41.         Quaternion targetRotation = Quaternion.Euler(0, rotationAmount, 0);
    42.         transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, Time.deltaTime * rotationSpeed);
    43.     }
    44. }
    45.