0
\$\begingroup\$

When my player jump beside walls and it reached the top of the wall, it's forcing the player to stop at the point of top of the wall. Like if my player y is higher than the y of the wall, it's making me stop. And I am using Character Controller. Is there a way to avoid this?

Here's a video

This is how I implemented my jump:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PlayerController : MonoBehaviour
{
    
    #region Variables

    [Header("Required Variables")]
    public LayerMask groundMask;
    public Slider energyBar;

    [Header("Movement and Gravity")]
    public float moveSpeed = 5f;
    public float jumpForce = 2.75f;
    public float gravity = -19.62f;
    public bool affectedByTimeScale = true;

    [Header("Configureable values")]
    public float maxEnergy = 100f;
    public float energyDrainAmount = 1f;
    public float energyGainAmount = 1f;
    public float cooldownBeforeGaining = 0.75f;
    
    CharacterController controller;
    Target target;
    Vector3 lastVelocityBeforeGrounded, velocity, move;
    float x, z, baseTime;
    bool jumping, running, grounded, moving;

    #endregion


    private void Awake() {
        if (affectedByTimeScale) baseTime = Time.fixedDeltaTime;
        else baseTime = Time.fixedUnscaledDeltaTime;

        controller = GetComponent<CharacterController>();
        target = GetComponent<Target>();

        lastVelocityBeforeGrounded = Vector3.zero;
        velocity = Vector3.zero;
        move = Vector3.zero;

        energyBar.maxValue = maxEnergy;
        energyBar.value = maxEnergy;
    }


    private void Update() {
        MyInput();
        Movement();
    }


    private void MyInput()
    {
        x = Input.GetAxis("Horizontal");
        z = Input.GetAxis("Vertical");
        jumping = Input.GetKeyDown(KeyCode.Space);
        running = Input.GetKey(KeyCode.LeftShift);

        if (x == 0 && z == 0) moving = false;
        else moving = true;
    }


    float counter = 0f;
    private void Movement()
    {
        Gravity();
        move = transform.right * x + transform.forward * z;
        CheckGround();

        if (jumping) Jump();
        if (running && moving && energyBar.value > 0)
        {
            move.x *= 1.5f;
            move.z *= 1.5f;
            energyBar.value -= energyDrainAmount;
            counter = 0f;
        }
        else
        {
            move.x /= 1.5f;
            move.z /= 1.5f;
            
            if (counter == 0f) counter = Time.time + cooldownBeforeGaining;
            if (Time.time > counter) energyBar.value += energyGainAmount;
        }

        controller.Move(move * baseTime * moveSpeed);
    }


    private void CheckGround()
    {
        grounded = controller.isGrounded;
        if (grounded)
        {
            // F = (V^2/2)
            if (lastVelocityBeforeGrounded.y < -10f)
            {
                float velY = Mathf.Abs(lastVelocityBeforeGrounded.y);
                float damage = Mathf.Pow(velY, 2) / 2;
                target.TakeDamage(damage);
            }

            velocity.y = -2f;
            lastVelocityBeforeGrounded = Vector3.zero;
        }
        else lastVelocityBeforeGrounded = velocity;
    }


    private void Jump()
    {
        if (grounded)
        {
            velocity.y += Mathf.Sqrt(jumpForce * -2f * gravity);
        }
    }


    private void Gravity()
    {
        velocity.y += gravity * baseTime;
        controller.Move(velocity * baseTime);
    }


}

This is my Camera Controller script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraController : MonoBehaviour
{
    
    #region Variables

    [Header("Required Variables")]
    public Transform orientation;
    public Transform player;

    [Header("Assignable Variables")]
    public float sensitivity = 65f;
    public bool affectedByTimeScale = true;
    
    float baseTime, xRotation, sensMultiplier;

    #endregion


    private void Awake() {
        if (affectedByTimeScale) baseTime = Time.fixedDeltaTime;
        else baseTime = Time.fixedUnscaledDeltaTime;

        Cursor.visible = false;
        Cursor.lockState = CursorLockMode.Locked;

        xRotation = 0f;
        sensMultiplier = 10f;
    }


    private void FixedUpdate() {
        float mouseX = Input.GetAxis("Mouse X") * baseTime * (sensitivity * sensMultiplier);
        float mouseY = Input.GetAxis("Mouse Y") * baseTime * (sensitivity * sensMultiplier);

        xRotation -= mouseY;
        xRotation = Mathf.Clamp(xRotation, -90f, 90f);

        orientation.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
        player.Rotate(Vector3.up * mouseX);

        transform.position = orientation.position;
        transform.rotation = orientation.rotation;
    }


}

This is my player setup: enter image description here

This is the variables inside camera controller: enter image description here

\$\endgroup\$
9
  • 1
    \$\begingroup\$ Besides the screen video, it would help a lot if you post your code on how you implement your jump. \$\endgroup\$
    – Zibelas
    Dec 27 '20 at 7:20
  • \$\begingroup\$ Can you take a look what's wrong in my code? or what makes it behave like that? \$\endgroup\$ Dec 28 '20 at 9:54
  • \$\begingroup\$ I tried to reproduce the error with a few stacked cubes but can only jump once. Now the question is, could your stairs maybe not smooth and there is a tiny edge you manage to land on one that allows you to do another jump? Your camera look script is just adjusting the camera and not the player, right? \$\endgroup\$
    – Zibelas
    Dec 28 '20 at 9:58
  • \$\begingroup\$ I have an orientation inside the player which makes the camera follow the position of player (because my player is not a parent of camera). \$\endgroup\$ Dec 28 '20 at 10:15
  • \$\begingroup\$ If you use a single cube and scale it (10, 10, 1) to make it a wall, can you still jump to its top? \$\endgroup\$
    – Zibelas
    Dec 28 '20 at 10:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.