hey, i’m writing some basic 2d movement rotation code and i’m having a problem. in the attached code, something is causing my left movement to always move right. if i disable the rotation code, the movement works correctly and if i disable the movement code, the rotation works correctly. I’m not sure why my values are not working as intended. any insight is appreciated ![]()
private var doublejump = 0;
private var onWall = 0;
var walkSpeed: float = 150.0;
var dashSpeed: float = 10.0;
var dashDistance: float = 5.0;
var runSpeed: float = 300.0;
var jumpSpeed : float = 200.0;
var jumpHeight: float = 16.0;
var maxFallingSpeed: float = 20.0;
var airControlFactor: float = 1.0;
var lookDirection : int;
var faceRight : Vector3 = Vector3(0,0,1);
var faceLeft : Vector3 = Vector3(0,0,-1);
var rotationSmoothing: float = 10.0;
var speedSmoothing: float = 5.0;
var gravity : float = 400.0;
private var moveDirection : Vector3 = Vector3.zero;
function OnTriggerEnter (other : Collider)
{
if (other.name == "Wall")
{
onWall = 1;
}
}
function OnTriggerExit (other : Collider)
{
if (other.name == "Wall")
{
onWall = 0;
}
}
function FixedUpdate ()
{
transform.position.z = 0;
}
function Update()
{
var controller : CharacterController = GetComponent(CharacterController);
var horizontalDirection = Input.GetAxisRaw ("Horizontal");
//movement
if (controller.isGrounded)
{
// We are grounded, so recalculate
// move direction directly from axes
doublejump = 0;
moveDirection = Vector3(horizontalDirection, 0, 0);
moveDirection = transform.TransformDirection(moveDirection) * walkSpeed;
print(moveDirection);
//single jump
if (Input.GetButtonDown ("Jump"))
{
moveDirection.y = jumpSpeed;
doublejump = 1;
print("singlejump");
}
}
//double jump
if (onWall == 0 controller.isGrounded == false)
{
if (Input.GetButtonDown ("Jump") doublejump == 1)
{
moveDirection.x = Input.GetAxis("Horizontal") * walkSpeed;
moveDirection.y = jumpSpeed;
doublejump = 0;
print("doublejump");
}
}
//wall jump
if (onWall == 1 controller.isGrounded == false)
{
print("touching the wall in air");
}
switch (horizontalDirection)
{
case 1:
lookDirection = 1;
break;
case -1:
lookDirection = 2;
break;
default:
break;
}
switch (lookDirection)
{
case 2:
transform.rotation = Quaternion.Slerp ( transform.rotation, Quaternion.LookRotation (faceLeft), Time.deltaTime * rotationSmoothing);
break;
case 1:
transform.rotation = Quaternion.Slerp ( transform.rotation, Quaternion.LookRotation (faceRight), Time.deltaTime * rotationSmoothing);
break;
default:
break;
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
controller.Move(moveDirection * Time.deltaTime);
}