Hey guys. As part of my game here, I’m trying to create a wall jump mechanic (i.e., “push” off the wall, whatever the most accurate terminology should be). The main issue is that I can’t figure out how to “force move” the CharacterController’s X or Z axis.
When the player jumps and hits a wall, the player should be able to press Space and jump in the wall’s opposite direction (off of its surface normal). I’m able to ascend on the Y axis, which is fine, just not the X or Z axis. I assume it should work like something similar to this:
moveVector = hit.normal (times) playerSpeed;
Although that doesn’t work.
I’m using the Character Controller physics instead of Rigidbody. This is a script I’m creating from scratch. I also used this guy’s tutorial for guidance.
If anyone can help me figure out what the issue is, or any suggestions to be able to jump off the wall in the X or Z axis directions, that would be great. Feel free to ask any questions!
This here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerGravity : MonoBehaviour
{
public float verticalVelocity;
public float gravity;
public float jumpForce;
public float playerSpeed;
private CharacterController cc;
private Vector3 moveVector;
private Vector3 lastMove;
//private bool runEnabled = true;
//private bool strafeEnabled = true;
void Start()
{
gravity = 14.0f;
jumpForce = 5.0f;
playerSpeed = 5.0f;
cc = this.GetComponent<CharacterController>();
}
void Update ()
{
if (cc.isGrounded)
{
verticalVelocity = -gravity * Time.deltaTime;
if (Input.GetKeyDown(KeyCode.Space))
{
verticalVelocity = jumpForce;
Debug.Log("JUMPED!");
}
}
else
{
verticalVelocity -= gravity * Time.deltaTime;
moveVector = lastMove;
}
moveVector = Vector3.zero;
moveVector.x = Input.GetAxis("Horizontal");
moveVector.z = Input.GetAxis("Vertical");
moveVector *= playerSpeed;
moveVector.y = verticalVelocity;
moveVector = transform.TransformDirection(moveVector);
cc.Move(moveVector * Time.deltaTime);
lastMove = moveVector;
/*if (runEnabled) { moveVector.x = Input.GetAxis("Horizontal"); }
else { moveVector.x = 0f; }
if (strafeEnabled) { moveVector.z = Input.GetAxis("Vertical"); }
else { moveVector.z = 0f; }*/
}
void OnControllerColliderHit(ControllerColliderHit hit)
{
if (!cc.isGrounded && hit.normal.y < 0.1f)
{
if (Input.GetKeyDown(KeyCode.Space))
{
Debug.DrawRay(hit.point, hit.normal, Color.red, 0.75f);
Debug.Log("WALL JUMPED");
verticalVelocity = jumpForce;
//transform.Translate(hit.normal * playerSpeed);
moveVector = hit.normal * playerSpeed;
}
}
else if (cc.isGrounded)
{
Debug.Log("Player grounded");
}
}
}