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

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.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Flight : MonoBehaviour
{
    public Transform rightController;
    public Transform leftController;
    private ParticleSystem rightControllerFlames;
    private ParticleSystem leftControllerFlames;
    public float moveSpeed;
    public Rigidbody rb;
    public AudioSource jetSound;
    private bool flamesNotPlaying;
    public Transform cam;
    private void Awake()
    {
        rightControllerFlames = rightController.GetComponentInChildren<ParticleSystem>();
        leftControllerFlames = leftController.GetComponentInChildren<ParticleSystem>();
    }

    private void Update()
    {
        if (leftControllerFlames.isPlaying == false && rightControllerFlames.isPlaying == false)
        {
            flamesNotPlaying = true;

            jetSound.Stop();
        }
       
        else
        {
            flamesNotPlaying = false;
        }

        var rightControllerFlamesEmission = rightControllerFlames.emission;
        var leftControllerFlamesEmission = leftControllerFlames.emission;

        rightControllerFlamesEmission.rateOverTime = ButtonIsPressed.rightTriggerValue * 100;
        leftControllerFlamesEmission.rateOverTime = ButtonIsPressed.leftTriggerValue * 100;
    }
    void FixedUpdate()
    {
        if (ButtonIsPressed.rightTriggerValue > 0.1f)
        {
            rightControllerFlames.Play(); 
           
            jetSound.volume = ButtonIsPressed.rightTriggerValue;

            if (!jetSound.isPlaying && !flamesNotPlaying)
            {
                jetSound.Play();
            }

            Vector3 forceVector = rightController.forward + rightController.right + rightController.up * ButtonIsPressed.rightTriggerValue * moveSpeed;

            rb.AddForce(forceVector);
        }

        else
        {
            rightControllerFlames.Stop();
        }

        if (ButtonIsPressed.leftTriggerValue < 0.01f && ButtonIsPressed.rightTriggerValue < 0.01f)
        {
            if (jetSound.isPlaying)
            {
                jetSound.Stop();
            }
        }


        if (ButtonIsPressed.leftTriggerValue > 0.1f)
        {
            leftControllerFlames.Play();

            jetSound.volume = ButtonIsPressed.leftTriggerValue;

            if (!jetSound.isPlaying && !flamesNotPlaying)
            {
                jetSound.Play();
            }

            Vector3 forceVector = leftController.forward + leftController.right + leftController.up * ButtonIsPressed.leftTriggerValue * moveSpeed;

            rb.AddForce(forceVector);
        }

        else
        {
            leftControllerFlames.Stop();
        }
    }
}

Your force vector looks like this:

Vector3 forceVector = rightController.forward + rightController.right + rightController.up * ButtonIsPressed.rightTriggerValue * moveSpeed;

Did you try just using

Vector3 forceVector = rightController.forward * ButtonIsPressed.rightTriggerValue * moveSpeed;

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).

1 Like

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.

1 Like