LayerMask wall running? I am trying to create a character controller wallrunning via LayerMask, please help me.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerMovement : MonoBehaviour {
public CharacterController controller;
public float speed = 12f;
public float runSpeed = 24f;
public float slideSpeedMultiplier = 2;
public float slideDuration = 1f;
public float slideDecreaseNumber = 1;
public float slideSpeedAfterDurationEnd = 8f;
private float chosenSlideDuration;
private float slideSpeed;
private float shiftSlideSpeed;
private float currentSpeed = 0;
public Animator slidingAnimator;
public bool sliding = false;
[Range(-100,0)]
public float gravity = -9.81f;
public float onWallGravity = 0;
private float normalGravity;
public float jumpHeight = 3f;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
public Transform wallCheckLeft;
public Transform wallCheckRight;
public float wallDistance = 0.4f;
public LayerMask wallMask;
public Animator speedIndicator;
public float highSpeed = 45f;
Vector3 velocity;
bool isGrounded;
bool isOnLWall;
bool isOnRWall;
void Start()
{
slideSpeed = speed * slideSpeedMultiplier;
shiftSlideSpeed = runSpeed * slideSpeedMultiplier;
chosenSlideDuration = slideDuration;
normalGravity = gravity;
}
// Update is called once per frame
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if(isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
isOnLWall = Physics.CheckSphere(wallCheckLeft.position, wallDistance, wallMask);
isOnRWall = Physics.CheckSphere(wallCheckRight.position, wallDistance, wallMask);
if(isOnLWall || isOnRWall)
{
//Debug.Log("on the wall");
gravity = onWallGravity;
}else{
gravity = normalGravity;
}
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
SlideCheck();
if (sliding && Input.GetKey(KeyCode.LeftShift))
{
controller.Move(move * shiftSlideSpeed * Time.deltaTime);
//Debug.Log("sliding with shift (Speed: " + shiftSlideSpeed + ")");
currentSpeed = shiftSlideSpeed;
}
else if (sliding)
{
controller.Move(move * slideSpeed * Time.deltaTime);
//Debug.Log("sliding (Speed: " + slideSpeed + ")");
currentSpeed = slideSpeed;
}
else if (Input.GetKey(KeyCode.LeftShift))
{
controller.Move(move * runSpeed * Time.deltaTime);
//Debug.Log("running (Speed: " + runSpeed + ")");
currentSpeed = runSpeed;
}
else
{
controller.Move(move * speed * Time.deltaTime);
//Debug.Log("walking (Speed: " + speed + ")");
currentSpeed = speed;
}
if(Input.GetButtonDown("Jump") && isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
SlideReset();
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
if(currentSpeed >= highSpeed)
{
speedIndicator.SetBool("Fast", true);
Debug.Log("Current speed: " + currentSpeed + " -FAST!");
}
else
{
speedIndicator.SetBool("Fast", false);
Debug.Log("Current speed: " + currentSpeed);
}
}
void SlideCheck()
{
if (Input.GetKeyDown(KeyCode.LeftControl))
{
SlideReset();
}
else if (Input.GetKey(KeyCode.LeftControl))
{
slideDuration -= Time.deltaTime;
slideSpeed -= slideDecreaseNumber * Time.deltaTime;
shiftSlideSpeed -= slideDecreaseNumber * Time.deltaTime;
sliding = true;
slidingAnimator.SetBool("Sliding", true);
if (slideDuration < 0)
{
slideSpeed = slideSpeedAfterDurationEnd;
shiftSlideSpeed = slideSpeedAfterDurationEnd;
}
}
else
{
sliding = false;
slidingAnimator.SetBool("Sliding", false);
}
}
void SlideReset()
{
slideSpeed = speed * slideSpeedMultiplier;
shiftSlideSpeed = runSpeed * slideSpeedMultiplier;
slideDuration = chosenSlideDuration;
}
}
Comment