Search code examples
c#.netunity-game-enginevirtual-reality

How to move object based on rotation another


I've got an issue in my VR project. I'm trying to move one object based on rotation of another one and first one should move only in kind of borders. I'm using a raycast to detect and control these objects. Both should go in x direction.

enter image description here

1 - Object that I rotate
2 - Object that should move
Any tips? Thanks in advance!


Solution

  • Well this looks like a lathe so I asume that you want to move the tool post over one axis when rotating the wheel. You want to "link" them, so what I would do is rotate the wheel using a method that also moves the tool post. Try this:

    public float ratio = 1.0f;
    public GameObject wheel;
    public GameObject tool;
    
    public void RotateWheel(float amount)
    {
         wheel.transform.Rotate(Vector3.forward * amount);
         tool.transform.Translate(Vector3.left * amount * ratio); 
    }
    

    Note the following:

    • Amount is passed as degrees.
    • Ratio is the ratio between wheel rotation and tool translation. At 1, for each degree, the tool moves 1 unit. Adjust it at your will. I assumed the orientation of your moving parts based on the model. Try changing the vector directions if they do not work as expected.