- Home /
Problem with moving / jumping
There is a slight problem with my script. If i walk up to a wall, and jump, it float / blocks the jump, i have no idea why, i even got into moving it with velocity instead of Translate, please don't ask me to use a non-friction material, it works, but i still want to be able to walk on a slope / surface without sliding off of it, And raycasting in every direction is probably resource consuming, is there a 'correct' way of doing this?
Code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
[RequireComponent(typeof(Rigidbody))] public class Player_Motor : MonoBehaviour {
private float speed; private Rigidbody rb; [HideInInspector] public Vector3 pVelocity; [Header("")] public bool CanSprint = true; public bool CanJump = true; [Header("")] public GameObject Camera; [Header("")] public Vector3 JumpHeight = new Vector3(0, 2f, 0); public float JumpForce = 0.55f; [HideInInspector] public bool JumpCooldown; [HideInInspector] public float JumpSecondsCooldown = 0.5f; [Header("")] public float CrouchSpeed = 1.5f; public float WalkSpeed = 4f; public float RunSpeed = 6f; [Header("")] public float RunHeadbobSpeed = 8.5f; public float CrouchHeadbobSpeed = 3.5f; private float hbSpeed; [Header("")] public float crouchHeight = 1.5f; public float crouchingSpeed = 10f; [Header("")] public float DamagePerSecond = 20f; public float MinFallTime = 3f; private bool sprinting; private bool crouching; [HideInInspector] public bool climbing; private float z; private float x; private float originalHeight; private float fallTime; private CapsuleCollider col; private void Start() { rb = GetComponent<Rigidbody>(); col = GetComponent<CapsuleCollider>(); originalHeight = col.height; speed = WalkSpeed; if (Camera != null) { hbSpeed = Camera.GetComponent<Player_Camera>().HeadbobSpeed; } } private void Update() { pVelocity = rb.velocity; if (!Grounded()) { if (!climbing) { fallTime += Time.deltaTime; } } else { if (fallTime >= MinFallTime) { if (GetComponent<Health>() != null) { GetComponent<Health>().TakeDamage((fallTime - MinFallTime) * DamagePerSecond); } Debug.Log("Ouch! Took " + (fallTime - MinFallTime) * DamagePerSecond + " Damage"); fallTime = 0f; } } if (Input.GetKeyDown(KeyCode.LeftShift) && CanSprint) { if (Camera != null) { if (Camera.GetComponent<Player_Camera>().useHeadbob) { Camera.GetComponent<Player_Camera>().HeadbobSpeed = RunHeadbobSpeed; } } CanJump = false; sprinting = true; speed = RunSpeed; } if (Input.GetKeyUp(KeyCode.LeftShift)) { if (Camera != null) { if (Camera.GetComponent<Player_Camera>().useHeadbob) { Camera.GetComponent<Player_Camera>().HeadbobSpeed = hbSpeed; } } CanJump = true; sprinting = false; speed = WalkSpeed; } if (crouching) { col.height = Mathf.Lerp(col.height, crouchHeight, Time.deltaTime * crouchingSpeed); } else { col.height = Mathf.Lerp(col.height, originalHeight, Time.deltaTime * crouchingSpeed); } if (sprinting) { crouching = false; } if (Input.GetKeyDown(KeyCode.LeftControl) && !sprinting) { crouching = true; CanSprint = false; if (Camera != null) { if (Camera.GetComponent<Player_Camera>().useHeadbob) { Camera.GetComponent<Player_Camera>().HeadbobSpeed = CrouchHeadbobSpeed; } } speed = CrouchSpeed; } if (Input.GetKeyUp(KeyCode.LeftControl)) { crouching = false; CanSprint = true; if (Camera != null) { if (Camera.GetComponent<Player_Camera>().useHeadbob) { Camera.GetComponent<Player_Camera>().HeadbobSpeed = hbSpeed; } } speed = WalkSpeed; } if (JumpCooldown && Grounded()) { JumpSecondsCooldown -= Time.deltaTime; if (crouching) { if (col != null) { col.height = 2f; crouching = false; } } if (JumpSecondsCooldown <= 0) { JumpSecondsCooldown = 0.5f; JumpCooldown = false; } } if (Input.GetKeyDown(KeyCode.Space) && Grounded() && CanStand()) { if (!JumpCooldown) { rb.AddForce(JumpHeight * JumpForce, ForceMode.Impulse); rb.velocity = JumpHeight * JumpForce; JumpCooldown = true; } } } private void FixedUpdate() { if (!climbing) { z = Input.GetAxis("Vertical"); } else { z = 0; } x = Input.GetAxis("Horizontal"); Vector3 forwardVel = transform.forward * speed * z; Vector3 horizontalVel = transform.right * speed * x; horizontalVel.y = rb.velocity.y; if (Camera != null) { transform.rotation = Quaternion.Euler(0, Camera.GetComponent<Player_Camera>().currentY, 0); } /* // when jumping (if in air) stop being able to move if (!Grounded()) { //apply velocity here return; } */ rb.velocity = forwardVel + horizontalVel; } public bool Grounded() { return Physics.Raycast(transform.position, Vector3.down, 1.5f); } public bool CanStand() { return !Physics.Raycast(transform.position, Vector3.up, 1.5f); } }
Answer by Santosh_Nair · Oct 22, 2018 at 05:15 PM
If the walls have rigidbody attached to it, is it kinematic? If not, can you try attaching a kinematic rigidbody to the walls? This is just a guess, because I think this issue might have to do more with some kind of physics calculation rather than your script. I mean maybe the colliders are applying some kind of drag or some kind of force.
Well, the rigidbody on the character DOES have angular drag idk if that will affect it, i will try what you said.
Nope, even after adding rigidbodys to the Walls, and making them like you said, it does not work. I adjusted the Angular Drag on my player, to no effect.
Sorry, I am not able to figure out! Hopefully someone else maybe be able to help.