drugsandwich
drugsandwich

Reputation: 79

Unity jumping fails while going against a wall

Okay im trying to make my object (player) to jump everything is okay until i go against a wall and keep going against (still W is down) i cant jump wen im hitting a wall if i stop walking he will be enable to jump i tried making the walls on touch to make the player to have velocity = zero but it does not work, i tried to add rigid body to the walls and freezing them in place, trying to make them kinematic does not work too .

I wish wen i go against walls and keep walking against them to be enable to jump. If you know how i can do that please share thanks .

Here is the move script:

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

public class MoveScript : MonoBehaviour {

private float speed;
private float jumpHight;
private float straffeSpeed;


private float fallMultiplier;
private Rigidbody rig;
private Collider coll;


// Use this for initialization
private void Awake()
{
    rig = GetComponent<Rigidbody>();
    coll = GetComponent<Collider>();
    straffeSpeed = 1.5f;
    fallMultiplier = 2.5f;
    speed = 10f;
    jumpHight = 4f;

}
void Start () {
    GroundCheck();
}

// Update is called once per frame
void Update () {
    Move();
    GroundCheck();
    BetterFall();

}
private void Move()
{
    float hAxis = Input.GetAxis("Horizontal") * straffeSpeed;
    float vAxis = Input.GetAxis("Vertical");
    Vector3 movement = new Vector3(hAxis, 0, vAxis) * speed * Time.deltaTime;
    rig.MovePosition(transform.position + movement);

    if (Input.GetKey(KeyCode.Space) && GroundCheck())
    {
        rig.velocity = Vector3.up * jumpHight;



    }
}
private bool GroundCheck()
{
    return Physics.Raycast(transform.position, -Vector3.up, coll.bounds.extents.y + 0.2f);
}

private void BetterFall()
{
    if(rig.velocity.y < 0)
    {
        rig.velocity += Vector3.up * Physics.gravity.y * (fallMultiplier - 1) * Time.deltaTime;
    }
}

Upvotes: 0

Views: 1774

Answers (3)

jwlewis
jwlewis

Reputation: 21

The physics Material answer from Sudarshan worked for me. Irritating frickin problem, but I created a zero friction material and put that on obstacle and player collider, jumps onto obstacles properly now.

Upvotes: 0

Sudarshan Doke
Sudarshan Doke

Reputation: 11

create new physics material with 0 friction (drag), add material to objects that are obstacles and add same physics material to collider of rigidbody (or whatever)

Upvotes: 1

Tom
Tom

Reputation: 2472

if (Input.GetKeyDown(KeyCode.Space) && GroundCheck())
{
    rig.velocity = Vector3.up * jumpHight;
}

I don't think you are doing this quite right. Try this:

if (Input.GetKeyDown(KeyCode.Space) && GroundCheck())
{
    rig.AddForce(Vector3.up * jumpHight, ForceMode.Impulse);
}

:-)

Upvotes: 1

Related Questions