- Home /
Wall Jump Scripting Help
Hi, Im trying to make a simple fast paced 3D plat former to learn the basics of unity. I wish to implement a wall jump that is relative to the surface the player is standing on so i dont have to create many diffrent implementations of what the player is standing on. This is my script so far...
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float speed;
public float jump = 20f;
private Rigidbody rb;
float timer = 0.0f;
bool isGrounded = true;
void Start()
{
rb = GetComponent<Rigidbody>();
}
private void Update()
{
bool player_jump = Input.GetButtonDown("Jump");
if (player_jump && isGrounded)
{
rb.AddForce(Vector3.up * jump);
}
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Ground"))
{
isGrounded = true;
}
if (collision.gameObject.CompareTag("Wall"))
{
isGrounded = true;
}
}
void OnCollisionExit(Collision collision)
{
if (collision.gameObject.CompareTag("Ground"))
{
isGrounded = false;
}
if (collision.gameObject.CompareTag("Wall"))
{
isGrounded = false;
}
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rb.AddForce(movement * speed);
}
}
So, My player is a ball and i wish to have it able to wall jump off of a wall. For now it jumps only up and i want to be able to grate a jump that applies a force off the wall. Like in Mario. So how do i create a wall jump that is relative to the surface that the player is standing on?
Hello.
Ok, nice project, nice script but... Whats the question then? Can you make a santence with your question ? :D
$$anonymous$$y player is a ball and i wish to have it able to wall jump off of a wall. For now it jumps only up and i want to be able to grate a jump that applies a force off the wall. Like in $$anonymous$$ario. So how do i create a wall jump that is relative to the surface that the player is standing on?
Answer by unity_2_3_VXKGFGtKtg · Oct 06, 2019 at 07:31 PM
hello there, i was asking about this from 2 days too no one replied, now what i did in my project that i used OnCollisionEnter and add two force one in the direction of wall normal and one in the upward direction it worked, but it has some bugs too since no one replying about wall jumping i thought i should tell you what solution in found
NOTE IT IS NOT PREFECT HAS BUGS
void OnCollisionEnter(Collision collision)
{
foreach (ContactPoint contact in collision.contacts)
{
if (!IsGrounded && contact.normal.y < 0.1f)
{
Debug.DrawRay(contact.point, contact.normal, Color.yellow, 5f);
if (walljump == true)
{
characterControl.RIGIDBODY.AddForce(contact.normal * 6f, ForceMode.Impulse);
characterControl.RIGIDBODY.AddForce(Vector3.up * 3f, ForceMode.Impulse);
}
}
}
}
I do get this error code from it... do you know how to fix it?!
PlayerController.cs(92,25): error CS0103: The name 'CharacterColtrol' does not exist in the current context
CharacterControl is script in my project from which i am using property to access rigidbody , in your case it'll be rb.AddForce
Could you post your entire script so I can see what I can change in $$anonymous$$e?! Because it is not working yet.
Answer by Blueshrimps · Mar 16 at 02:10 PM
Hello Liraseth! I was having similar problems with wall jumping. I at first tried using raycasting to determine the hit point, but when that didn't work, I reverted to a much simpler method. This is for character controllers, though.
private void OnControllerColliderHit(ControllerColliderHit hit) { if(!controller.isGrounded && controller.collisionFlags == CollisionFlags.Sides && Input.GetKey(KeyCode.Space)) { (your move vector).y = jumpForce; transform.rotation = Quaternion.Euler(0f, transform.rotation.eulerAngles.y + 180f, 0f); float getYRotation = transform.rotation.eulerAngles.y; transform.position += transform.forward Time.deltaTime (your move speed); lockMove = true; }
This method makes the player turn around 180 degrees, so it works even if you hit the wall at a diagonal angle. for the "lock move" bool, only allow your player to rotate if lock move is false, and then set it to false when grounded.
The way this works it that when the player does a wall jump, they will flip around and stay in that direction, and trying to move the player back toward the wall will just move them further away. if you need to get the player's y rotation, the float variable in there can track it. I really hope this works for you and is compatible with your script!