Quantcast
Channel: Questions in topic: "addforce"
Viewing all articles
Browse latest Browse all 1264

Rigidbody.velocity seems to be breaking jumping physics

$
0
0
I'm currently creating a character controller script and i'm facing 2 problems: 1. When falling off an edge, the player falls back to the ground in an extremely slow manner. This can't have anything to do with my gravity because when I jump successfully, i'm falling normally. 2. Sometimes, when jumping, the player somehow gets stuck in the air right at the start and, again, falls back slowly. The jump height is intended. Code: using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerController : MonoBehaviour { public Rigidbody rb; public Transform player; RaycastHit hit; public float speed; public float force; public bool jumping; private void Start() { rb = rb.GetComponent(); } private void Update() { if(Physics.Raycast(transform.position, Vector3.down, out hit, 1f)) { jumping = false; Debug.Log(hit.distance); if (Input.GetKeyDown(KeyCode.Space)) { jump(); } } } private void FixedUpdate() { if (!jumping) { Vector3 move = player.transform.forward * Input.GetAxis("Vertical") + player.transform.right * Input.GetAxis("Horizontal"); move = move * speed * Time.deltaTime; move = Vector3.ClampMagnitude(move, 20f); rb.velocity = move; } } void jump() { jumping = true; rb.AddForce(force * Time.fixedDeltaTime * Vector3.up); } } Video: [Here][1] [1]: https://drive.google.com/file/d/1XZ8CrTOmn2gZoU5qlnGeQwUeyGU9b_y3/view?usp=sharing

Viewing all articles
Browse latest Browse all 1264

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>