- Home /
Question by
isaackhan99 · Apr 14, 2019 at 08:32 PM ·
collisionvector3jumpwall
Trying to Wall jump
I am having a problem with trying to wall jump. Whenever I try to jump from a wall, instead of bouncing off in the opposite direction, it propels me in the forward direction. I am wondering if anyone can help me why this is happening. Here is my script for a First person Controller. I have created a Character Controller for the player. It also contains the functions for the movement, jump and crouch. The wall jump function is created at the bottom. I think the problem lies with the last line of the script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FPSController2 : MonoBehaviour
{
public float speed = 2f;
public float sensitivity = 2f;
public float jumpForce = 4f;
CharacterController player;
private bool hasJumped;
private bool isCrouched;
public GameObject eyes;
float moveFB;
float moveLR;
float rotX;
float rotY;
float vertVelocity;
void Start()
{
player = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
moveFB = Input.GetAxis("Vertical") * speed;
moveLR = Input.GetAxis("Horizontal") * speed;
rotX = Input.GetAxis("Mouse X") * sensitivity;
rotY -= Input.GetAxis("Mouse Y") * sensitivity;
rotY = Mathf.Clamp(rotY, -60f, 60f);
Vector3 movement = new Vector3(moveLR, vertVelocity, moveFB);
transform.Rotate(0, rotX, 0);
eyes.transform.localRotation = Quaternion.Euler(rotY, 0, 0);
movement = transform.rotation * movement;
player.Move(movement * Time.deltaTime);
if (Input.GetButtonDown("Jump"))
{
hasJumped = true;
}
if (Input.GetButtonDown("Crouch"))
{
if (isCrouched == false)
{
player.height = player.height / 2;
isCrouched = true;
}
else
{
player.height = player.height * 2;
isCrouched = false;
}
}
ApplyGravity();
}
private void ApplyGravity()
{
if (player.isGrounded == true)
{
if (hasJumped == false)
{
vertVelocity = Physics.gravity.y;
}
else
{
vertVelocity = jumpForce;
}
}
else
{
vertVelocity += Physics.gravity.y * Time.deltaTime;
vertVelocity = Mathf.Clamp(vertVelocity, -50f, jumpForce);
hasJumped = false;
}
}
private void OnControllerColliderHit(ControllerColliderHit hit)
{
if (!player.isGrounded && hit.normal.y < 0.1f)
{
if (Input.GetButtonDown("Jump"))
{
vertVelocity = jumpForce;
Debug.Log("Jumped");
Vector3 movement = hit.normal * speed;
}
}
}
}
Comment
Your answer
Follow this Question
Related Questions
How to Fix Player Jump Against Wall 1 Answer
Camera looking through objects when touching 1 Answer
Collision problem 2 Answers
Stop Dashing through walls 1 Answer
Box collider didn't jump with player 1 Answer