- Home /
AddForce Jump Doesn't Work
Hello , I don't know why Jump don't work // im new in unity so i need some help Note : walk is good!!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
CharacterController controller;
private Rigidbody rb;
private Animator playeranim;
public float speed = 18.0f;
public float gravity = -12f;
public float jump = 15f;
private float verticalVelocity;
public bool IsOnGround = true;
Vector3 movedirection = Vector3.zero;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
playeranim = GetComponent<Animator>();
controller = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
Jump();
walk();
}
void walk()
{
float leftrightzft = Input.GetAxis("Horizontal") * 3f;
if (IsOnGround == true)
{
movedirection = new Vector3(leftrightzft, 0, 2f);
movedirection *= speed;
}
controller.Move(movedirection * Time.deltaTime);
}
void Jump()
{
if (IsOnGround&&Input.GetKeyDown(KeyCode.Space))
{
rb.AddForce(Vector3.up * jump, ForceMode.Impulse);
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Ground"))
{
IsOnGround = true;
}
}
}
You should add rigidbody component Or Any Rigidbody constraints is marked
Answer by unity_ek98vnTRplGj8Q · Feb 06, 2020 at 05:09 PM
Do not use both a character controller AND a rigidbody on the same object. Choose one and stick to it. Also, the reason that your jump doesn't work is because you have isKinematic
checked on your rigidbody. Your character can be controlled directly or by physics but not by both at the same time. You likely want to control your character directly, (controlling your character by adding forces is much harder and offers less control). If you want to "Add a force" to a kinematic rigidbody or a character controller, you have to simulate this yourself by keeping track of the velocity of the character, and then moving it by that velocity every frame.
public float jumpStrength;
private Vector3 _velocity;
private float velocityBleedSpeed = 2f;
void Update(){
Jump();
controller.Move(_velocity * Time.deltaTime);
_velocity = Vector3.Lerp(_velocity, Vector3.zero, velocityBleedSpeed*Time.deltaTime);
}
void Jump(){
if(IsOnGround && Input.GetKeyDown(KeyCode.Space)){
_velocity += new Vector3(0,jumpStrength, 0);
}
}
It's Work very thanks x 1241431 You don't know how tired I am because of this thing , but i want to use gravity 2 !
$$anonymous$$ake your own gravity ;)
public float jumpStrength;
private Vector3 _velocity;
private float gravity = 9.81f;
private float velocityBleedSpeed = 2f;
void Update(){
Jump();
controller.$$anonymous$$ove(_velocity * Time.deltaTime);
if (!IsOnGround) _velocity.y -= gravity * Time.deltaTime;
_velocity = Vector3.Lerp(_velocity, Vector3.zero, velocityBleedSpeed*Time.deltaTime);
}
void Jump(){
if(IsOnGround && Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space)){
_velocity += new Vector3(0,jumpStrength, 0);
IsOnGround = false;
}
}
very thanks man,I lost two days of my time because of this thing !! and thanks lol.
Your answer
Follow this Question
Related Questions
AddForce Movement Constraint 0 Answers
AddForce to Camera 1 Answer
moving with rigidbody without acceleration 0 Answers
Player appears to teleport instead of adding force 2 Answers
My movement function with jump won't work with collider function 1 Answer