简体   繁体   中英

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;
    }
}
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);
}

:-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM