So I’ve got a functioning wall jump mechanic (which you’re welcome to use). It’s all done in the WallJump method. I’m not getting errors, but the player doesn’t turn around. The bottom line in the method should cover that. I’ll add comments of my previous attempts. Nothing is working! The player just keeps looking in the same direction.
using UnityEngine;
using System.Collections;
public class Movement : MonoBehaviour
{
//Public Variables
//Movement Variables
public float moveSpeed = 3.0f;
public float sprintSpeed = 6.0f;
public float crouchSpeed = 1.5f;
public float jumpPower = 7.5f;
public float wallJumpPushOffPower = 3.0f;
public float wallJumpPushUpPower = 7.5f;
public float minWallCollisionAngle = 60.0f;
public float gravity = -20.0f;
//Input Variabes
public string moveAxisX = "Horizontal";
public string moveAxisZ = "Vertical";
public string sprintButton = "Sprint";
public string crouchButton = "Crouch";
public string jumpButton = "Jump";
//Hidden Variabes
//Private Variables
//Movement Variables
private Vector3 movement;
//Component Variables
private CharacterController controller;
void Start ()
{
//We have to tell the controller variable what CharacterController we want to be changing.
//In this case it's the CharacterController on the object that this script is attached to.
controller = this.GetComponent<CharacterController>();
}
void Update ()
{
//We are calling the PlayerMovement method; it holds all the movement functionality for organization.
PlayerMovement ();
}
void OnControllerColliderHit (ControllerColliderHit collision)
{
if(!controller.isGrounded)
{
if(Vector3.Angle(this.transform.forward, collision.normal) > minWallCollisionAngle)
if(Input.GetButtonDown(jumpButton))
WallJump(collision.normal);
}
}
void PlayerMovement ()
{
//If the player is touching the ground he will be able to move and jump
if(controller.isGrounded)
{
movement = new Vector3(Input.GetAxis(moveAxisX), 0, Input.GetAxis(moveAxisZ));
//We have to turn the movement vector into world space instead of local.
movement = transform.TransformDirection(movement);
//If the player is holding down the sprint button we'll multiply movement by sprintSpeed.
if(Input.GetButton(sprintButton))
movement *= sprintSpeed;
//If the player isn't holding the sprint button but IS holding the crouch button we'll multiply movement by crouchSpeed.
else if(Input.GetButton (crouchButton))
movement *= crouchSpeed;
//Otherwise we'll multiply movement by moveSpeed because the player isn't sprinting.
else
movement *= moveSpeed;
//If the player presses the jump button we'll set the players y movement to the value of jumpPower.
if(Input.GetButtonDown(jumpButton))
movement.y = jumpPower;
}
//Whether or not the player is touching the ground we'll still move the player down at the speed of the gravity variable.
movement.y += gravity * Time.deltaTime;
//By this point the movement variable is calculated correctly and we can now move the player in its direction.
controller.Move(movement * Time.deltaTime);
}
void WallJump (Vector3 normal)
{
movement = Vector3.Reflect(transform.forward, normal);
movement *= wallJumpPushOffPower;
movement.y = wallJumpPushUpPower;
//this.transform.Rotate(0, 180, 0);
//this.transform.eulerAngles += new Vector3(0, 180, 0);
this.transform.rotation = Quaternion.Inverse(this.transform.rotation);
}
}