- Home /
Rotate object based on rotation of SteamVR Controller?
I am trying to make a dial on a machine (similar to ones you may find on a stove), where the user can rotate it, but only on one axis (in this case, the Z axis) and cannot move it from its location. Here is an image of my dial:
It should be noted that the scale of the model is 100, and it is a child of an empty GameObject whose scale is 1, which is a child of a model whose scale is 0.0075.
Here is the code I have so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Valve.VR.InteractionSystem;
[RequireComponent(typeof(Interactable))]
public class Dial914 : MonoBehaviour
{
Hand playerHand;
[SerializeField] Rigidbody dial;
// Use this for initialization
void Start()
{
}
private void HandAttachedUpdate()
{
dial.MoveRotation(Quaternion.Euler(0, 0, -playerHand.transform.rotation.z * transform.localScale.z)); //I actually don't know why we have to negate the hand rotation here, but it works
}
protected virtual void OnAttachedToHand(Hand hand)
{
playerHand = hand;
}
}
This mostly works, but you can only rotate it halfway around and it starts to get buggy if you try to go further than that.
Here is a gfycat of the result:
@toxxicxmodz Hello! Can you go into a little more detail on how you ended up getting this working correctly. I've been struggling trying to get an accurate working dial for some time now. I tried your above code and it still produced an odd result.. it would stop rotating after a certain point.. plus the dial is always reset in this case to the position of the hand when ever you grab it again.. finally.. if you actually move your hand around (side to side, up and down) it will cause the dial to spin as well.
Really wanting an easy way to use the S$$anonymous$$mVR Interaction System with a Dial. Very surprised this isnt an included script.
Answer by toxxicxmodz · Nov 11, 2018 at 10:15 PM
Found the answer:
Vector3 eulerRotation = new Vector3(0, 0, playerHand.transform.eulerAngles.z);
transform.rotation = Quaternion.Euler(eulerRotation);
Answer by clint205 · Jan 31, 2019 at 02:01 AM
@toxxicxmodz Hello! Can you go into a little more detail on how you ended up getting this working correctly. I've been struggling trying to get an accurate working dial for some time now. I tried your above code and it still produced an odd result.. it would stop rotating after a certain point.. plus the dial is always reset in this case to the position of the hand when ever you grab it again.. finally.. if you actually move your hand around (side to side, up and down) it will cause the dial to spin as well.
Really wanting an easy way to use the SteamVR Interaction System with a Dial. Very surprised this isnt an included script.
Your answer
Follow this Question
Related Questions
Set a rotation relative to position between two objects. 0 Answers
How to write a script to disable position and rotation tracking for VR 0 Answers
Steam VR touchpad to rotate object 1 Answer
Change rotation of an object based on an HTC Vive Controller 1 Answer
Camera viewport transformation from one world to the rotated world. 1 Answer