/Free Cake for anyone who can solve this/
I’m trying to write a Walljumping function using Vector.Reflect, and I managed to getting to work somewhat, but the reflection only works when the character is facing the forward direction (if the camera is facing behind him).
//=====================================================================
// ApplyWalljump
// -Not Working all the way yet........................................
//=====================================================================
function ApplyWallJump ()
{
var jumpButtonPressed = Input.GetButtonDown("Jump");
//Checks to see if the player is against a wall and trips a flag
if ((controller.collisionFlags & CollisionFlags.CollidedSides) != 0 )
{isAgainstWall = true;}
else
{isAgainstWall = false;}
// We must actually jump against a wall for this to work
if (!isJumping)
return;
if (isAgainstWall && jumpButtonPressed && !controller.isGrounded )
{
//isWalljumping = true;
var reflectedMoveDirection : Vector3 = Vector3.Reflect (moveDirection, transform.forward);
reflectedMoveDirection.y = y;
reflectedMoveDirection.y -= gravity * Time.deltaTime;
transform.rotation = Quaternion.LookRotation(Vector3(reflectedMoveDirection.x, 0, reflectedMoveDirection.z));
controller.Move(reflectedMoveDirection * Time.deltaTime);
}
}
How can I adjust the Reflection Direction so that the character will go into the opposite direction when he jumps against the wall, no matter where he jumps from?
If you need to see how the movement is set up, check it below:
//=====================================================================
// CalculateMovement
//=====================================================================
function CalculateMovement()
{
//Get the Inputs
x = Input.GetAxis("Horizontal");
z = Input.GetAxis("Vertical");
y = moveDirection.y;
var jumpButton = Input.GetButton("Jump");
var jumpButtonPressed = Input.GetButtonDown("Jump"); //SMH
controller = GetComponent(CharacterController);
var cameraTransform : Transform = Camera.main.transform;
//Get the forward vector from the camera
var forward : Vector3 = cameraTransform.TransformDirection(Vector3.forward);
forward.y = 0;
forward = forward.normalized;
// Right vector relative to the camera
var right = Vector3(forward.z, 0, -forward.x);
//Make sure that the movement is relative to the camera
moveDirection = x * right + z * forward;
//Speed the Character up
moveDirection *= speed;
//Orient the Player Accordingly
if (moveDirection != Vector3.zero && !onLedge)
{
transform.rotation = Quaternion.LookRotation(Vector3(moveDirection.x, 0, moveDirection.z));
}
//This is needed, otherwise the character can't move while jumping
moveDirection.y = y;
//MAKE SURE JUMP ALWAYS COME AFTER ROTATION!!!!
if (jumpButtonPressed && controller.isGrounded && canJump)
{
ApplyJump(jumpHeight);
isJumping = true;
}
else if (jumpButtonPressed && !controller.isGrounded && canJump && canDoubleJump)
{
ApplyJump(jumpBoost);
canJump = false;
}
//This Area is For Gliding
if (jumpButton && !controller.isGrounded && moveDirection.y <= 0.0 && canGlide)
{isGliding = true;}
else
{isGliding = false;}
//If not on the ground
if (!controller.isGrounded)
{
isAirborne = true;
}
//This checks to see if the player is on the ground, or if the player is on the ground and decides to walk off the side of something
else if (controller.isGrounded && moveDirection.y <= 0.0)
{
isAirborne = false;
isJumping = false;
canJump = true;
isWalljumping=false;
moveDirection.y = 0;
}
}
//=====================================================================
// ApplyMovement
//=====================================================================
function ApplyMovement()
{
if (!isGliding && (!onLedge && !onWall))
{
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
}
else
{
// Apply gravity at a reduced rate
moveDirection.y -= glideGravity * Time.deltaTime;
}
// Move the controller
if (!onLedge && !onWall && !isWalljumping)
{
controller.Move(moveDirection * Time.deltaTime);
}
}