0

I'm having a problem with the rotation of a game object. I'm able to rotate the object in the direction/angle of another game object. But after the first rotation the game object i'm rotating based on the delta rotation of the source game object It starts rotating in a different direction because it has been rotated once already I guess.

So is there a way so that after the first rotation I can reset the rotation so It moves again in the angle/direction the other object is rotating?

This is my current code below, i'm not a math expert so I might be doing things wrong .. So the basic thing I want is that the second game object rotates in the same direction the base game object rotates but I don't want the rotation values to be equal, the rotation values doesn't have to be the same, it only has to rotate in the same angle/direction as the base game object rotates. TiltAgainstObject is the source game object. The script is attached to the second game object.

Any help is welcome :).

public class Tilt : MonoBehaviour {

public GameObject TiltAgainstObject;
private float rotX;
private float rotY;
private float rotZ;
private float rotW;
private float deltaX;
private float deltaY;
private float deltaZ;
private float deltaW;

// Use this for initialization
void Start () {

    rotX = TiltAgainstObject.transform.rotation.x;
    rotY = TiltAgainstObject.transform.rotation.y;
    rotZ = TiltAgainstObject.transform.rotation.z;
    rotW = TiltAgainstObject.transform.rotation.w;
}

// Update is called once per frame
void Update () {

    if(OVRInput.Get(OVRInput.Button.One))
    {
        deltaX = TiltAgainstObject.transform.rotation.x - rotX;
        deltaY = TiltAgainstObject.transform.rotation.y - rotY;
        deltaZ = TiltAgainstObject.transform.rotation.z - rotZ;
        deltaW = TiltAgainstObject.transform.rotation.w - rotW;
        gameObject.transform.localRotation = new Quaternion(gameObject.transform.rotation.x + deltaX,
                                                       gameObject.transform.rotation.y + deltaY,
                                                       gameObject.transform.rotation.z + deltaZ,
                                                       gameObject.transform.rotation.w + deltaW);
    }
    rotX = TiltAgainstObject.transform.rotation.x;
    rotY = TiltAgainstObject.transform.rotation.y;
    rotZ = TiltAgainstObject.transform.rotation.z;
    rotW = TiltAgainstObject.transform.rotation.w;
}

}

LordrAider
  • 73
  • 9

2 Answers2

0

The question seems a bit confusing, bur if you want the second gameobject (where your script is) to rotate with the same direction of the first object you should do something like this:

public class Tilt : MonoBehaviour {

public GameObject TiltAgainstObject;
private float rotX;
private float rotY;
private float rotZ;
private float rotW;
private float deltaX;
private float deltaY;
private float deltaZ;
private float deltaW;

// Use this for initialization
void Start () {

    rotX = TiltAgainstObject.transform.rotation.x;
    rotY = TiltAgainstObject.transform.rotation.y;
    rotZ = TiltAgainstObject.transform.rotation.z;
    rotW = TiltAgainstObject.transform.rotation.w;

    deltaX = 0;
    deltaY = 0;
    deltaZ = 0;
    deltaW = 0;

}

// Update is called once per frame
void Update () {
    rotX = TiltAgainstObject.transform.rotation.x;
    rotY = TiltAgainstObject.transform.rotation.y;
    rotZ = TiltAgainstObject.transform.rotation.z;
    rotW = TiltAgainstObject.transform.rotation.w;

    this.gameObject.transform.localRotation = new Quaternion(rotX + deltaX,
                                                       rotY + deltaY,
                                                       rotZ + deltaZ,
                                                       rotW + deltaW);


}

This way the second object will rotate in the same direction of the first object, if you want to change the speed of rotation just modify the deltas.

If you want the rotation to occur only when you press the button.One just pust it inside the if statement instead:

if(OVRInput.Get(OVRInput.Button.One))
    {
      this.gameObject.transform.localRotation = new Quaternion(rotX + deltaX,
                                                       rotY + deltaY,
                                                       rotZ + deltaZ,
                                                       rotW + deltaW);
    }
João Martins
  • 332
  • 2
  • 10
  • Sorry for the confusing question, my logic in the sample "works" but only for the first rotation. The second time I rotate it , it doesn't rotate in the same direction anymore, only the first rotation. I guess it's because the second time you rotate the rotation euler angles of the second object aren't 0,0,0 anymore? Any idea how to make the second time you press the button to rotate that the second object also rotates in the same direction correctly?. – LordrAider Jun 21 '16 at 08:24
0

I think the problem is that you're assigning the world coordinate rotation to your local coordinate localRotation, a typo maybe? Besides that though, I'm not sure why you're editing the components of the Quaternion directly, generally it's better to just add them together and use Unity's functions since dealing with 4 dimensions is often error-prone. You might try something like this:

public class Tilt : MonoBehaviour {

  public GameObject TiltAgainstObject;
  private Quaternion prevRotation;
  private Quaternion currentRotation;

  void Start()
  {
    prevRotation = TiltAgainstObject.transform.rotation;
  }

  void Update()
  {
    currentRotation = TiltAgainstObject.transform.rotation;
    if (OVRInput.Get(OVRInput.Button.One))
      gameObject.transform.rotation *= Quaternion.RotateTowards(prevRotation, currentRotation, 1000f);
    prevRotation = currentRotation;
  }
cjmarsh
  • 292
  • 2
  • 12