Finding difference in rotations of objects [SOLVED]

Hello, I have been updating the code for the ‘drag to move’. I added some cool stuff that gives it some more realism. I just ran into a problem which should be easy to fix… however it seems that my code isn’t doing what I expected at all.

Here is the situation:

For the features I have added, I needed to child the object being picked up to another object. That is all fine and dandy. I have the parent object face the player (This is one of my upgrades where it makes it more real. Instead of the object dangling and spinning when the player spins around, it stays in position like it would in real life in your hands)… problem is this:

As soon as you pick the object up, it of course changes its rotation to look at the player. That is what we want… however we don’t want it to change its rotation. We want it to keep its natural rotation (when it was just lying there) and face the player that way as he holds it and turns.

I have a solution but as I said… it is not doing what I expected. On pickup, I am taking the player controllers localRotation.y and subtracting the object’s localRotation.y from that number.

I logically thought this would give the difference of rotation. By applying the difference to the object you are picking up, it should offset the rotation so then when they do pick it up it shifts it’s own local rotation while its parent(holder object) faces the player. Creating a seamless appearance of the object never rotating at all but adhering to the face the player rule.

Essentially. I need to know why the code seems to randomly rotate the object and not make it appear to stay at the same rotation.

//this is me trying to find and apply the difference of rotations...not working as I had hoped...

heldobj.transform.parent = holder.transform;
	  heldobj.transform.localRotation.y = charactercontroller.transform.rotation.y-heldobj.transform.localRotation.y;


then later in the code:

//this part works and makes the holder that the heldobj is a parent of face the player

var look = playercontroller.transform.position - holder.transform.position;
					look.y = 0;
					holder.transform.rotation = Quaternion.LookRotation (look, Vector3.up);

It sounds like you’re saying that you want the object to face the player but you don’t want the object to face the player
which is slightly confusing

what happens if you just set the object’s rigidbody.isKinematic while the player’s holding it? That would remove the physics movements and when you drop it you could unset isKinematic and regain physics.

I need to keep physics so it interacts with the environment… Yes this in confusing lol

Here it is in the best way I can say it. When you currently use the drag to move script. When you pick up an object and have it held in front of you, when you spin the player around, the object does not spin as if you are holding it in your hands in front of you, it doesn’t spin with the player. It stays in front of him, but its rotation doesn’t change. So it looks like its spinning in the opposite direction in front of him when he turns.

So, I childed the object (when you pick it up) to an object called holder… the holder object always faces the player. This way the object you are holding rotates with the player because it is a child of the holder object which is now forced to always face “look at” the player. The problem is this…

When you pick the object up by clicking on it and holding the mouse, it instantly changes its rotation to face the player. Its rotation shouldn’t visually change because you picked the object up with the rotation it already had. BUT… because the object has a parent that is forced to face the player (so it doesn’t do the weird spinning thing while he turns)…the object rotates with its parent.

What I am needing to do is offset the objects local rotation so that it ‘appears’ to start at the same rotation it was when you pick it up before you grab it. This way I can keep the function that makes its parent face the player when you spin but it won’t suddenly rotate the object itself ‘visually’.

So the trick is to counteract the parents rotation when it faces the player so it seems to never change its own rotation visually. Even though to do this we will have to rotate it in opposition to the rotation the parent “holder” object needs to face the player.

LOL, I know this seems crazy but in actually physical form in a game it is important and the trouble seems to be in comparing the rotation the “holder/parent” object gets to face the player and apply the reverse to the child object that you are visually picking up and moving in the game. I am thinking that my issue may be from confusion about euler angles, transform.localRotation and the quaternion form that the look at player is sending.

I am running debugs and the numbers for the rotation don’t match what is showing in the editor window. Strange.

That is confusing :slight_smile: But, I think maybe I know what you’re asking about, so I’ll take a guess at an answer here.

First of all, accessing the elements of the rotation field directly probably isn’t going to help you. The rotation fields are quaternions; the elements of a quaternion representing a rotation do have a specific meaning, but it’s probably not what you’re expecting, especially if you’re trying to think of them as ‘angles’ of some sort. The rotation axis and angle are encoded in the elements of the quaternion, but the elements are not angles themselves.

In any case, it sounds like what you want is to reset the object’s local rotation so that when it becomes parented to the player, its apparent orientation (that is, its world orientation) stays the same. This can be done as follows (pseudocode):

targetObject.transform.localRotation =
    inverse(parentObject.transform.rotation) *
    targetObject.transform.rotation;

I think I got that right (it should be something like that, at least).

Thank you… You got it sort of. I was just doing the same thing in a different way…with the exact same result…

this seems to be working (the first time you click it to pick it up)

now the problem is that when you drop it and pick it up again, it rotates extra.

hmmm

Sorry, I figured it was something dumb! ICK!

I accidentally had the set parent line AFTER the rotation fix line.

Works wonderfully now!! YES!

Once I finish the updates on the click drag and move script I will post them.

So far I added an auto drop function that makes the object drop from your grip if it gets too far away behind another object.

I also added a hold middle mouse button rotate object function so the player can manipulate the object more directly.

Next up is going to be a throw the object function where the longer you hold the right mouse button down the harder it throws etc.

Then I need to code physics damage.

A nice thing to code would be some sort of vertex manipulation on objects hitting with physics so I can dent them and stuff. That part is beyond me at the moment for sure lol.