0

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; } } 
1
  • Depending on the wall material/physics you might be running into friction. If you hold against a wall and try to jump the friction from the wall might stop you. Depending on the type of game you are making your level objects shouldn't cause friction. Commented Dec 11, 2017 at 15:25

3 Answers 3

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

:-)

Sign up to request clarification or add additional context in comments.

2 Comments

does not matter i tried it and it just blast into air even if i change the jump hight to the lowest im just gonna use the character controller:D
Oops, I think I realised why! You should use GetKeyDown instead of GetKey. This way it will only jump once instead on continuously jumping whilst the space bar is held down.
1

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)

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.