- Home /
Rotate Animation In-Game?
Hello fellow Unity users! I've come back to Unity answers to ask for help with something that I can't seem to get working. I'm trying to rotate my Player in-game when he goes down a slope or up one. I used to be able to do this with ease using Player.transform.rotation = Quaternion.Euler(0, 0, Rotation); however ever since I've began to create my animations in engine using the record button in the animation window, I can no longer rotate in-game.
Every time I try and rotate it quickly goes back to it's original rotation. I can rotate the player when the game is not playing(so when the animation is not playing) and he'll stay in the rotation through the whole level, but when I try to do it while playing it doesn't work. I hope I'm making some sense here.
My Unity version is kinda old (it's 4.3.4), so maybe it's easier in the newer versions. However I can't upgrade at all since I'm in development and it always ruins my game's controls and many other things.
So how can I rotate in-game? Is it possible in my old version of Unity? Can I rotate while ignoring animations? I need help!
Here's my script:
public float Rotation;
public GameObject Player;
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player") {
Player.transform.rotation = Quaternion.Euler(0, 0, Rotation);
}
}
}
i think a possible answer to this is in your code you run the animation before rotating your player. did you try rotating it before running the animation?
Another thing that could mess you up is if you recorded your parent object that holds all of the child object. That means it will override your rotation script with your animation.
EX:
Player //If you have this selected when your recording it will mess things up
Root//You want to only record from this object and below
Spine
...
At least this has caused me troubles in the past.
@Vice_Versa: It works when I do that. The problem is that I need to be able to do it while still running the animations.
@wesleywh: That's exactly what I did. You see the character is divided into separate body parts(it's a 2D game), and the parent object is the head of the character. So I thought to use the head as the part to record. So do I have to re-record everything or is there another solution?
Answer by nisovin · Jul 20, 2015 at 11:30 PM
Try creating two objects, one the child of the other. The animation should go on the child object, and when you rotate the player in script, you should rotate the parent object. This way the animation will only affect the rotation of the child object, which will in turn be affected by the rotation of the parent.
This kinda works. First I tried to copy the parent object which holds the animations and controls, and only keep the animator and the sprite renderer, and child it to the original. Then from the original which is now the parent, control the animations that are on the child. This was able to get it to rotate in-game, however for some weird reason is messed up my controls and made the game drop in frame rate. It probably had to do with the scripting.
Now I tried to child the original to a empty game object. However this one doesn't rotate correctly. You see when I rotate it from it's Z Rotation axis on the empty game object. It makes the Character rotate far up or down depending on the way it rotates.
Any idea why this might be?
Answer by wesleywh · Jul 21, 2015 at 06:20 PM
If thats your problem then all you need to do is remove the points on your animation that are watching your head. You might be able to select all those points labeled head and delete them.
I guess it wasn't the cause of my problem, since I've just deleted the head points yet the Player still doesn't rotate. I tried attaching the player to an empty game object and then rotating the game object, but the Player moves in the direction that he is rotating. So if I rotate the Z axis to -30.53 he moves down, and if I rotate 30.53 he moves up. This wouldn't be a real issue if he didn't glitch through the ground.
I've searched up this problem and found this page: http://answers.unity3d.com/questions/123542/move-and-rotate-on-a-fixed-axis.html But I could not get the solution to work for me. Any ideas how I can fix this issue?