- Home /
Problem with character facing towards movement direction
Hello everyone, I'm pretty new to Unity, so I have quite some trouble with various parts, this being one of them.
I am currently making a 2,5D Sidescroller, I have a following camera set up, an own playermodel with proper animations. Following various tutorials (the one I started with was never finished), I am now trying to have my character face the direction he is moving in. A common problem from what I have seen, but even with the other similiar topics I didn't manage to get it fixed on my end.
I currently use the following code to control the animations and the direction my character should face:
public class AnimationChooser : MonoBehaviour {
Animator anim;
void Awake ()
{
anim = GetComponent<Animator> ();
}
void Update ()
{
float running = Input.GetAxis ("Vertical");
float jumping = Input.GetAxis ("Jump");
float dashing = Input.GetAxis ("Dash");
float dir = Input.GetAxisRaw ("Vertical");
if (dir != 0)
{
if (dir > 0)
{
transform.eulerAngles = Vector3.up * 90;
}
else
{
transform.eulerAngles = Vector3.up * 270;
}
}
anim.SetFloat ("Speed", running*running);
anim.SetFloat ("Jumping", jumping*jumping);
anim.SetFloat ("Dashing", dashing*dashing);
}
}
Oddly enough, the character nicely faces the proper direction for each movement direction (left and right), but he only moves to the right, no matter what I press.
Also, the reason for using the vertical axis and multiplying with 90 and 270 is that I had to turn my model by 90 degrees in the first place. Without that, he moved into and away from the camera instead of left and right.
Any help on this is highly appreciated, so thanks in advance.
Answer by Saiura · Jun 07, 2014 at 08:48 AM
This is less than an answer, more a little push (sorry about that, but I really need to get this solved)
I currently have it like this with the turning:
public float smooth = 15.0F;
void LateUpdate () {
// Rotation
Quaternion targetf = Quaternion.Euler(0, 270, 0); // Vector3 Direction when facing frontway
Quaternion targetb = Quaternion.Euler(0, 90, 0); // Vector3 Direction when facing opposite way
if (Input.GetAxisRaw ("Vertical") < 0.0f) // if input is lower than 0 turn to targetf
{
transform.rotation = Quaternion.Lerp(transform.rotation, targetf, Time.deltaTime * smooth);
}
if (Input.GetAxisRaw ("Vertical") > 0.0f) // if input is higher than 0 turn to targetb
{
transform.rotation = Quaternion.Lerp(transform.rotation, targetb, Time.deltaTime * smooth);
}
}
just changed the Slerp for a Lerp to get a waaaay faster rotation (no idea why, but it works).
My old problem still persists though... the character does turn into the direction of the input key, but only moves in one direction... If I press the right arrow key, the character faces and moves towards the right... If I press the left arrow key, the character faces to the left but "moon-walks" to the right.
How can I fix that?
Would it be possible to just create inverse movement for the left and right arrow key for a certain facing-direction?
Edit:
I solved it now, went to the FPSInputController
There I put:
if (Input.GetKey("d") || Input.GetKey("right"))
{
directionVector = directionVector * directionLength;
}
if (Input.GetKey("a") || Input.GetKey("left"))
{
directionVector = -directionVector * directionLength;
}
else
{
directionVector = directionVector * directionLength;
}
right where there was just
directionVector = directionVector * directionLength;
before.
Answer by Stygtand · Jun 05, 2014 at 11:20 AM
Im not sure which way is which from the looks of your script. But this is the solution i use. Attach this to a child gameobject of the object that has the input controller. That way, your character controller (which controls the inputs) is moving back and forth, yet your child (this rotation game object) hold its relative transform position, but you change the rotation for the object and its child (which holds the model). This script also turn the object slowly. Change smooth float to a lower float for faster turn speed.
public float smooth = 2.0F;
void LateUpdate () {
// Rotation
Quaternion targetf = Quaternion.Euler(0, 0, 0); // Vector3 Direction when facing frontway
Quaternion targetb = Quaternion.Euler(0, 180, 0); // Vector3 Direction when facing opposite way
if (Input.GetAxisRaw ("Vertical") < 0.0f) // if input is lower than 0 turn to targetf
{
transform.rotation = Quaternion.Slerp(transform.rotation, targetf, Time.deltaTime * smooth);
}
if (Input.GetAxisRaw ("Vertical") > 0.0f) // if input is higher than 0 turn to targetb
{
transform.rotation = Quaternion.Slerp(transform.rotation, targetb, Time.deltaTime * smooth);
}
}
Well, I created an empty, put the script onto the empty and then dragged it on my player (in the hierarchy), I am pretty sure I did miss something or did something wrong cause... well... it doesn't do a thing
First you have your game object with your input. then create a empty game object, you can call it rotation if you wish. Put this script onto it. Then place your mesh render / your model in a child of this.
[Character] : insert your controllers here
[Rotation] : insert rotation
[$$anonymous$$odel] : insert model
This way, when your char object moves its transformation. All the child objects of the character will follow the transform. [Rotation] scripts then rotate itself and childs below. Lastly the model shows the [model], which we move from character and rotate from [rotation].
well, my character model holds all the character scripts. the Input, $$anonymous$$otor, Character-controller and some self made scripts are all in the very same model at the moment, I will see to change it up a bit.
I also just tried to put your script into the same spot I had my previous rotation script (with some slight number changes). funny enough, the character now rotates when I move in the other direction. Though when I turn around, the character turns very slow, even at smooth on 0,1f. Also another problem is that while turning, he moves into the direction he faces during the turn, causing him to fall off the platform.
The problem with the movement direction flipping consists like that still though
I also tried adding a new object, giving it the input, motor, character controller scripts, making my empty for the rotation a child of it and placing my character model als child of the rotation-empty. Though now, ins$$anonymous$$d of moving probably, my character floats above the ground and as soon as I move, he flies faaar away in a slight curve to the side.
As another option... is there a way to say something like, swap the direction the character moves at a specific degree of turning? like... if character is turned by X degrees, inputkey Y moves in - direction on the vertical axis?
I added another comment below about the results
I think you got it at the second last comment. but you need to speed up the rotation speed, and force your z axis to 0 while turning. try set smooth to like 10 or 15f,
do a "transform.poistion.z = 0f" this will force position z to be 0 always.
the reason for rotation script to be on a child of the controls is, that even though the character turns, the controls stays the same. I hope it helps.