Search Unity

Resolved How do I get the player to fly in the direction of his hand?

Discussion in 'VR' started by jlorenzi, Sep 3, 2021.

  1. jlorenzi

    jlorenzi

    Joined:
    May 2, 2021
    Posts:
    292
    I'm trying to make an iron man simulation in VR but I don't know how I would make the player go the direction that his hand is facing. The code I have so far is this.

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Flight : MonoBehaviour
    7. {
    8.     public Transform rightController;
    9.     public Transform leftController;
    10.     private ParticleSystem rightControllerFlames;
    11.     private ParticleSystem leftControllerFlames;
    12.     public float moveSpeed;
    13.     public Rigidbody rb;
    14.     public AudioSource jetSound;
    15.     private bool flamesNotPlaying;
    16.     public Transform cam;
    17.     private void Awake()
    18.     {
    19.         rightControllerFlames = rightController.GetComponentInChildren<ParticleSystem>();
    20.         leftControllerFlames = leftController.GetComponentInChildren<ParticleSystem>();
    21.     }
    22.  
    23.     private void Update()
    24.     {
    25.         if (leftControllerFlames.isPlaying == false && rightControllerFlames.isPlaying == false)
    26.         {
    27.             flamesNotPlaying = true;
    28.  
    29.             jetSound.Stop();
    30.         }
    31.        
    32.         else
    33.         {
    34.             flamesNotPlaying = false;
    35.         }
    36.  
    37.         var rightControllerFlamesEmission = rightControllerFlames.emission;
    38.         var leftControllerFlamesEmission = leftControllerFlames.emission;
    39.  
    40.         rightControllerFlamesEmission.rateOverTime = ButtonIsPressed.rightTriggerValue * 100;
    41.         leftControllerFlamesEmission.rateOverTime = ButtonIsPressed.leftTriggerValue * 100;
    42.     }
    43.     void FixedUpdate()
    44.     {
    45.         if (ButtonIsPressed.rightTriggerValue > 0.1f)
    46.         {
    47.             rightControllerFlames.Play();
    48.            
    49.             jetSound.volume = ButtonIsPressed.rightTriggerValue;
    50.  
    51.             if (!jetSound.isPlaying && !flamesNotPlaying)
    52.             {
    53.                 jetSound.Play();
    54.             }
    55.  
    56.             Vector3 forceVector = rightController.forward + rightController.right + rightController.up * ButtonIsPressed.rightTriggerValue * moveSpeed;
    57.  
    58.             rb.AddForce(forceVector);
    59.         }
    60.  
    61.         else
    62.         {
    63.             rightControllerFlames.Stop();
    64.         }
    65.  
    66.         if (ButtonIsPressed.leftTriggerValue < 0.01f && ButtonIsPressed.rightTriggerValue < 0.01f)
    67.         {
    68.             if (jetSound.isPlaying)
    69.             {
    70.                 jetSound.Stop();
    71.             }
    72.         }
    73.  
    74.  
    75.         if (ButtonIsPressed.leftTriggerValue > 0.1f)
    76.         {
    77.             leftControllerFlames.Play();
    78.  
    79.             jetSound.volume = ButtonIsPressed.leftTriggerValue;
    80.  
    81.             if (!jetSound.isPlaying && !flamesNotPlaying)
    82.             {
    83.                 jetSound.Play();
    84.             }
    85.  
    86.             Vector3 forceVector = leftController.forward + leftController.right + leftController.up * ButtonIsPressed.leftTriggerValue * moveSpeed;
    87.  
    88.             rb.AddForce(forceVector);
    89.         }
    90.  
    91.         else
    92.         {
    93.             leftControllerFlames.Stop();
    94.         }
    95.     }
    96. }
    97.  
     
    Last edited: Sep 5, 2021
  2. SPollari

    SPollari

    Joined:
    Sep 8, 2021
    Posts:
    6
    Your force vector looks like this:

    Code (CSharp):
    1. Vector3 forceVector = rightController.forward + rightController.right + rightController.up * ButtonIsPressed.rightTriggerValue * moveSpeed;
    2.  
    Did you try just using

    Code (CSharp):
    1. Vector3 forceVector = rightController.forward * ButtonIsPressed.rightTriggerValue * moveSpeed;
    2.  
     
  3. colinleet

    colinleet

    Joined:
    Nov 20, 2019
    Posts:
    189
    Two big errors here at least:

    1) (From above) Yup your motion vector is trying to move the player in three perfectly perpendicular angles simultaneously. You're confusing adding the x, y, z position floats with up, right, and forward directions of the controller. Just use the forward vector of the controller.

    2) Also make sure to multiply the force vector by fixed delta time, or else your "speed" will effectively be multiplied as meters per frame instead of meters per second (201 mph * the actual speed you want in m/s when playing at 90 hz... Instead of 1 * the speed you want in m/s).
     
    jlorenzi likes this.
  4. colinleet

    colinleet

    Joined:
    Nov 20, 2019
    Posts:
    189
    Also you're not handling gravity here at all (unless you disabled it)… The player will fall unless you also add the player's up vector to their movement (1 g of force will keep them at the same altitude, more and they'll rise, less and they'll sink.
     
    Last edited: Nov 16, 2021
    jlorenzi likes this.