- Home /
Jump from wall to wall (wallrunning)
What can i add so that i can jump from wall to wall in this script? New to programming so i would appreciate any help i can get! :D
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.Characters.FirstPerson;
public class Wallrun : MonoBehaviour {
private bool isWallR;
private bool isWallL;
private RaycastHit hitL;
private RaycastHit hitR;
private int jumpCount = 0;
private RigidbodyFirstPersonController cc;
private Rigidbody rb;
// Use this for initialization
void Start () {
cc = GetComponent<RigidbodyFirstPersonController>();
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update () {
if (cc.Grounded)
{
jumpCount = 0;
}
{
if (Physics.Raycast(transform.position, transform.right, out hitR, 1))
{
if (hitR.transform.tag == "Wall")
{
isWallR = true;
isWallL = false;
jumpCount += 1;
rb.useGravity = false;
StartCoroutine (afterRun());
}
}
if (Physics.Raycast(transform.position, -transform.right, out hitL, 1))
{
if (hitL.transform.tag == "Wall")
{
isWallL = true;
isWallR = false;
jumpCount += 1;
rb.useGravity = false;
StartCoroutine (afterRun());
}
}
}
}
IEnumerator afterRun()
{
yield return new WaitForSeconds(0.5f);
isWallL = false;
isWallR = false;
rb.useGravity = true;
}
}
Comment
Best Answer
Answer by KittenSnipes · May 06, 2018 at 09:17 AM
@Sawrrhd I found this nice script that should be good enough to modify. Should help you out a bit. Cheers! :D
public float speed = 8;
public float jumpForce = 10;
public float gravity = 25;
private Vector3 moveVector;
private Vector3 lastMove;
private float verticalVelocity;
private CharacterController controller;
private void Start()
{
controller = GetComponent<CharacterController>();
}
private void Update()
{
moveVector = Vector3.zero;
moveVector.x = Input.GetAxis("Horizontal");
moveVector.z = Input.GetAxis("Vertical");
if (controller.isGrounded)
{
verticalVelocity = -1;
if (Input.GetKeyDown(KeyCode.Space))
{
verticalVelocity = jumpForce;
}
}
else
{
verticalVelocity -= gravity * Time.deltaTime;
moveVector = lastMove;
}
moveVector.y = 0;
moveVector.Normalize();
moveVector *= speed;
moveVector.y = verticalVelocity;
controller.Move(moveVector * Time.deltaTime);
lastMove = moveVector;
}
private void OnControllerColliderHit(ControllerColliderHit hit)
{
if (!controller.isGrounded && hit.normal.y < 0.1f)
{
if (Input.GetKeyDown(KeyCode.Space))
{
verticalVelocity = jumpForce;
moveVector = hit.normal * speed;
}
}
}
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Can you help with the character controller 1 Answer
How to make obect move only if its on top of another object 2 Answers