- Home /
My CharacterController does not walk and face in the same direction
When I go left my CharacterController turns left which is good but he walks to the right. Going right works fine and while I know why hes walking backwards I cannot figure out how to fix it. This is a 2D game movement is on the X axis.
var speed : float = 4.0;
//var speedo : float = -4.0;
var gravity : float = 20.0;
private var velocity : Vector3 = Vector3.zero;
var controller : CharacterController;
function Update ()
{
if (controller.isGrounded)
{
velocity = GetHorizontalMovement();
velocity *= speed;
}
velocity.y -= gravity * Time.deltaTime;
controller.Move (velocity * Time.deltaTime);
if(Input.GetAxisRaw("Horizontal") > 0)
transform.rotation = Quaternion.Euler(0, 0, 0);
else if(Input.GetAxisRaw("Horizontal") < 0)
transform.rotation = Quaternion.Euler(0, 180, 0);
}
function GetHorizontalMovement()
{
var direction : Vector3 = Vector3 (Input.GetAxisRaw("Horizontal"), 0, 0);
return transform.TransformDirection (direction);
}
Answer by superluigi · Jun 13, 2013 at 08:41 PM
Ok figured it out although I'm not 100% sure how I did it.
var speed : float = 4.0;
var gravity : float = 20.0;
private var velocity : Vector3 = Vector3.zero;
var controller : CharacterController;
function Update ()
{
if (controller.isGrounded && Input.GetAxisRaw("Horizontal") > 0)
{
transform.rotation = Quaternion.Euler(0, 0, 0);
velocity = GetHorizontalMovement();
velocity *= speed;
}
else if(controller.isGrounded && Input.GetAxisRaw("Horizontal") < 0)
{
transform.rotation = Quaternion.Euler(0, 180, 0);
velocity = GetHorizontalMovement();
velocity *= speed;
}
else if(controller.isGrounded)
{
velocity = GetHorizontalMovementZero();
}
velocity.y -= gravity * Time.deltaTime;
controller.Move (velocity * Time.deltaTime);
}
function GetHorizontalMovement()
{
var direction : Vector3 = Vector3 (1, 0, 0);
return transform.TransformDirection (direction);
}
function GetHorizontalMovementZero()
{
var direction : Vector3 = Vector3 (0, 0, 0);
return transform.TransformDirection (direction);
}
Answer by jerobinson · Jun 13, 2013 at 07:06 AM
The problem is that your horizontal movement changes between + / - when pressing left / right. At the same time, the character also changes rotation depending on what key you press. (Pressing right; character faces right and moves forward, pressing left; character faces left and moves backwards). So rather than changing the characters velocity between positive or negative, just make it equal to speed when there is input:
if(Input.GetAxis("Horizontal") > 0f) {
velocity.z = speed;
}
EXAMPLE SCRIPT:
var speed : float = 4.0;
//var speedo : float = -4.0;
var gravity : float = 20.0;
private var velocity : Vector3 = Vector3.zero;
var controller : CharacterController;
function Update ()
{
if (controller.isGrounded)
{
if(Input.GetAxisRaw("Horizontal") > 0) {
velocity.x = speed;
}
}
velocity.y -= gravity * Time.deltaTime;
controller.Move (velocity * Time.deltaTime);
if(Input.GetAxisRaw("Horizontal") > 0)
transform.rotation = Quaternion.Euler(0, 0, 0);
else if(Input.GetAxisRaw("Horizontal") < 0)
transform.rotation = Quaternion.Euler(0, 180, 0);
}
so what your saying is change my GetHorizontal$$anonymous$$ovement function? sorry I'm still confused also do you really want me to mess with the z axis or is that a typo?
Sorry, yes. I meant to say velocity.x = speed;. Your GetHorizontal$$anonymous$$ovement function is making the player move backwards whenever he looks left. The most basic way that you could fix this would be to change var direction : Vector3 = Vector3 (Input.GetAxisRaw("Horizontal"), 0, 0);
to var direction : Vector3 = Vector3 (1f, 0, 0);
I have added a made the adjustments needed to your code, anded added it to my answer above. ;)
I already tried that but it doesn't work what happens is the player just starts walking in one direction non stop even if I let the key go.
You would need to set the speed.x to 0 again when the key is released. Oh well, you fixed it anyways. Good Luck ;)
Answer by Bradders1504 · Jun 13, 2013 at 08:06 AM
Make the Input.GetAxisRaw("Horizontal")
a negative value like -(Input.GetAxisRaw("Horizontal"))
. For the GetHorizontalMovement() function.
Thanks for the answer but it didn't work. All this did was switch the problem now he walks backwards when facing to the right
I see, was a quick answer, thanks for commenting back
GetAxisRaw and GetAxis work on a scale between 1 and -1 adding a negative to the input method would only change wich side is negative and positive.