localRotation problems

Hello,
I’m hoping someone could explain this problem I’m having with localRotation.
I’m using following bit of code to get my player model to jump up and rotate at the same time (so it looks like it is running on the roof). This bit works fine.

//Jump Up
transform.localPosition = Vector3.Lerp(endPoint,startPoint, (Time.time - startTime) / duration);
transform.localRotation = Quaternion.Lerp(transform.localRotation, target,Time.deltaTime * smooth);

The model moves up to Y -2 and rotates along it’s local Z axis by 180

The problem is that when I try to get it to drop down, I can’t get the rotation to work (the transform is fine).

//Jump Down
transform.localPosition = Vector3.Lerp(endPoint, startPoint, (Time.time - startTime) / duration);
transform.localRotation = Quaternion.Lerp (transform.localRotation, targetDown,Time.deltaTime * smooth);

It just appears to totally ignore the Z rotation and the other values I enter are off by a lot / don’t relate to the actual values or axis, but when I jump again (back to the roof), the rotation is correct.
Now I realize that when it’s on the roof, the +Z axis will now be pointing in the other direction (towards the screen)as will the +X axis (pointing left instead of right), but that doesn’t seem to explain the faults.

VAR / values used.
startPoint=Vector3 0,-10,0 //Ground position
endPoint=Vector3 0,-2,0//Roof position
target = Quaternion.Euler (0, 0, 180);//Roof rotation
targetDown = Quaternion.Euler (0, 0, 180);//Ground rotation (also tried various other values)

Basically, when I rotate the player 180 on the Z axis, I would expect that if I rotated it back another 180, it would return back to the original rotation, but that’s just doesn’t happening. So am I doing something wrong, as I don’t really understand the different kinds of rotations (never heard of a Quaternion before) and I can’t find anything that explains it from a novice point of view. I’m at artist, not a coder but I’m slowly getting into Unity and it’s always a buzz to get something up and running on the screen.

Any help / pointers would be greatly appreciated as I am at a total loss now.

Thanks,
Mike

Setting the value of transform.rotation or transform.localRotation sets the rotation in space directly. That is to say, it doesn’t add extra rotation on to what the object already has. The targetDown value isn’t adding another 180º to the current Z rotation, it is just setting the rotation to the same value it already has.

You can use transform.Rotate to perform incremental rotations. If this doesn’t sort you out, perhaps you can describe in some more detail what is going on in the game. Although you’ve given good information about the code, it is difficult to visualise what is going on in the game when the player reaches the roof, etc. (Is the game a side scroller, third person… ?)

Hi Andeeee,
First off, thanks for replying (really appreciated).

I’ve attached a picture to try to explain the problem a bit better (with values during the different states).
The setup is I’ve made a prefab that takes care of the player with the following structure.
*MainPlayer
-PlayerMasterAxis (Not used and empty)
–PlayerModel
–MainCamera (Default, nothing added)

MainPlayer: The prefab itself with scripts assigned to it for creating the tunnel and rotating around the origin (it only moves in the positive Z axis)
PlayerModel: The actual model with the jump script assigned to it(which just waits for the screen to be pressed and then raises / lower the model by 7 units and rotates it 180 on its Z Axis (initial rotation values are 0,0,0).

The player prefab(MainPlayer) rotates around the Z axis while it constantly moves forward, so the Y Axis of the player model acts like a hand of a clock, which is why I need to do rotations from its local Z axis.

Hope that makes sense.

I set up the target / targetDown as public variables so I could play around while the game was running, but after the initial jump on to the roof, the Z axis simple doesn’t want to know (when using the code in the first post).

On the jump down, if I set the targetDown value to 0,360,0 it lands on it’s feet, but facing the camera (which is totally wrong as rotating on the Y axis should just make it spin like a spinning top).

If I just do a straight transform.rotate (0,0,180) on the down jump, it works as expected, but it’s an instant rotation and I need it to smoothly do it over time.

Thanks again for your help,
Mike.

Status Var Value Inspector Shows
1: Start position (0,0,0) 0,0,0
2: Jump up (0,0,180) 0,0,180
3: Jump Down (0,0,0) 0,0,180

You could try using the initial approach (with Quaternion.Lerp) but use a different method of setting the target orientation. Essentially, you want the character to remain looking forward, but his upward direction should change to be the opposite of the current direction. You can use Quaternion.LookRotation to achieve this:-

targetRotation = Quaternion.LookRotation(transform.forward, -transform.up);

Thanks for that, but I can’t get it to do it over time (it’s another instant rotation), so I’m thinking of just fudging it for now.
I’ll do a one off animation for the the player dropping off the roof and then at the end of the animation, change the rotation in code and continue with the running animation. Poor way of doing it I know, but I don’t like taking all your time up with this fault and I can always come back to it later (I want to keep the momentum up on my prototype).

Thanks again for your help.

Mike.