I am making a VR game, and need to move the player’s hand forward in the direction it is aimed at. Currently, I have enter co{ public GameObject hand; public float movementSpeed = 5f; public bool isExtending = false; public bool isRetracting = false; public int extendCount = 0; // Update is called once per frame void Update() { float horizontalInput = Input.GetAxis("Horizontal"); float verticalInput = Input.GetAxis("Vertical"); if (isExtending) { extendCount++; if (extendCount > 60) { isExtending = false; isRetracting = true; return; } transform.position = transform.position + new Vector3(movementSpeed * Time.deltaTime, 0, 0); } else if (isRetracting) { extendCount--; if (extendCount == 0) { isRetracting = false; return; } transform.position = transform.position + new Vector3(movementSpeed * Time.deltaTime * -1, 0, 0); } else { var LeftControllers = new List<UnityEngine.XR.InputDevice>(); UnityEngine.XR.InputDevices.GetDevicesWithRole(UnityEngine.XR.InputDeviceRole.LeftHanded, LeftControllers); foreach (var device in LeftControllers) { bool gripValue; if (device.TryGetFeatureValue(UnityEngine.XR.CommonUsages.gripButton, out gripValue) && gripValue) { isExtending = true; } } } } }de here
It extends, but only in one direction. I know that the problem is that I have nothing to detect where it is facing, but I don’t know how to do that (this is my first VR game, so I’m not very knowledgeable with it)
from the best I can tell, you’re not referencing a rotation at all. transform.rotation
reads the game code as a Quaternion
, even though it shows Euler Angles
in the Inspector. So to set the rotation, you can’t say
transform.rotation = new Vector3(10f,0f,50f);
you’ll need to say
transform.rotation = Quaternion.Euler(10f,0f,50f)
if you want to declare a particular way to go. Other than that you’ll need to reference an Objects forward
transform.rotation = object1.transform.forward;
or something like this:
Quaternion targetRotation = Quaternion.LookRotation(object2Position, Vector3.up);
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, turnSpeed);
Rotation is a fun category, once you get into the meat and potatoes of it