When i try to climb a wall of a more height than the max slope variable in the character controller, but if i jump and go forward, the distance from the player and the upper side of the wall its less than max slope, it must be possible to climb, but when i go in front of the wall and hold W and jump, the player goes down quickly and glitchy. Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMove : MonoBehaviour
{
private CharacterController controller;
public float speed = 12f;
public float gravity = -9.81f;
public float jumpHeight = 3f;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
float x;
float z;
Vector3 velocity;
bool isGrounded;
bool canMoveX;
// Start is called before the first frame update
private void Awake()
{
controller = GetComponent();
}
// Update is called once per frame
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if (isGrounded && velocity.y < 0f)
{
velocity.y = -2f;
}
if (canMoveX && isGrounded)
{
x = Input.GetAxis("Horizontal");
}
z = Input.GetAxis("Vertical");
if (!isGrounded)
{
StartCoroutine("CheckIsGrounded");
}
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * speed * Time.deltaTime);
if(Input.GetButton("Jump") && isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2 * gravity);
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
IEnumerator CanMoveX()
{
x = 0f;
yield return new WaitForSecondsRealtime(0.1f);
canMoveX = true;
}
IEnumerator CheckIsGrounded()
{
yield return new WaitForSecondsRealtime(0.01f);
if (isGrounded)
{
StartCoroutine("CanMoveX");
}
else
{
canMoveX = false;
}
}
}
↧
When i jump in front of a wall and going forward, the player go down glitchy (character controller)
↧