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);
}
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);
}
Make the Input.GetAxisRaw("Horizontal")
a negative value like -(Input.GetAxisRaw("Horizontal"))
. For the GetHorizontalMovement() function.
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);
}