- Home /
Rotate walking animation
This has been getting to me for a few days now guys, I have read countless pieces of documentation on slerp and things alike but I cannot figure it out. Basically, my character moves right when I press the D key. Everything is fine, the animations are perfect. However, I wish to have my character move right also, I have done this but I want the character to rotate 180 degrees and face the way he is moving. At the moment he is just walking backwards (he is moving in the correct direction). What would be the best way to counter this problem? Thanks in advance!
Please note: When I rotate him, he just adopts the previous controls and moves the opposite way to which he should.
If you need anymore information I will be here for a while :)
Script:
var acceleration : float = 100;
var move : float;
var doubleJump : float = 0;
var z : float;
var health : int = 100;
var jumpDelay : boolean;
var shoulder : Transform;
var shoulder1 : Transform;
var hasSword : boolean;
var hasGun : boolean;
var target : Transform;
var rotation : float = 180;
// weapon variables for switch statement.
var weapon : int = 0;
var sword : Transform;
var gun : Transform;
function Update () {
// Set the gravity of the level and the starting animation.
Physics.gravity = Vector3( 0, -200, 0);
animation.CrossFadeQueued("idle");
// If the D key is pressed, set the acceleration to 100 and play the walk animation. If mouse button 0 is pressed and the player has acquired the
// sword, blend the sword animation at the same time.
if( Input.GetKeyDown(KeyCode.D))
{
acceleration = 100;
animation.CrossFade("walk");
}
if( Input.GetKeyDown(KeyCode.Space) && jumpDelay == false )
{
Jump();
animation.CrossFade("jump");
animation.CrossFadeQueued("idle");
jumpTimer();
}
// If the player is not walking, play the idle animation.
if (Input.GetKeyUp(KeyCode.D) || Input.GetKeyUp(KeyCode.A))
{
animation.CrossFade("idle");
}
// Stop the moonwalking bug. If the player hits both A and D, without this code the accelaration will equal 0 but still play the walking animation, hence
// the moon walking bug. Additionally, if the player releases one of the keys, the character will move in the right direction.
if (Input.GetKey(KeyCode.D) && Input.GetKey(KeyCode.A))
{
acceleration = 0;
animation.CrossFade("idle");
}
else if (Input.GetKey(KeyCode.A))
{
acceleration = 100;
move = Input.GetAxis("Horizontal") * acceleration * Time.deltaTime;
transform.Translate (0, 0, move);
transform.Rotate(0,180,0);
}
// Movement script allowing the player to move.
move = Input.GetAxis("Horizontal") * acceleration * Time.deltaTime;
transform.Translate (0, 0, move);
}
// The simple jump code with the delay boolean/function.
function Jump(){
rigidbody.velocity.y = 175;
}
function jumpTimer(){
if (animation.IsPlaying("jump"))
jumpDelay = true;
yield WaitForSeconds(3);
jumpDelay = false;
}
I have removed some of the script so it isn't tedious to read. There are no errors, some syntax may be missing.
why do you want the character to rotate 180degrees rotate him 90 degress and what eactly do you want the object to do when you press vertical axis
Thanks for the comment, basically my game is a side scroller. You can move left and right and jump. That is the limits of the game but when I implement the code to rotate him 180 degrees to face left, it's like the pivot does not rotate with the mesh.
hmm does the mesh rotate or is it just part of the body or other what rotates when you press a or d
Answer by whydoidoit · Nov 03, 2012 at 11:39 AM
Translate works by default in the space of the object, so when you rotate the object all of your controls are backwards - this is one of the reasons I don't like using these calls, it's not obvious what's going on! Pass Space.World as a fourth parameter to your Translate calls.
ok so the Space.World will help me rotate.. well... everything including the pivot?
Space world will mean that when you move the character it will move without worrying about whatever rotation it has - it will move in world units.
Actually I think you just want to set the rotation - not do a .Rotate - that's just turning it all the time you press the key.
Ah no
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.A))
{
acceleration = 100;
move = Input.GetAxis("Horizontal") * acceleration * Time.deltaTime;
transform.Translate (0, 0, move, Space.World);
transform.rotation.eulerAngles.y = 180;
}
So you will need to do a transform.rotation.eulerAngles.y = 0 in the D key code.
Answer by MihailS · Aug 29, 2017 at 01:29 PM
Hey guys. We got into this issue , which for me sound really close with what you had. Basically there is a series of animations that have to play based on some input. Idle>Run>... etc So far so good yet there are also the 180 degree and 135 degree turn etc. Here the things become really tangled. We kinda spent several days now trying to find a solution, yet we are stuck with that snap back to original direction which brakes all the flow of movement and of course makes the game unusable. :( @Nercoe : Was your problem similar with this? If so , did you manage to fix it? If yes , can you give me some more detailed hints? Thank you ! Mihail
Your answer
Follow this Question
Related Questions
How can I have a line stay between to points as one or both of them moves? 1 Answer
Can the animation editor create local rotational data? 3 Answers
Adding animation clips via script 2 Answers
Rotating Door 2 Answers
Bone rotation during animation? 2 Answers