- Home /
Character don`t jump
Character dont jump
using UnityEngine;
// MoveBehaviour inherits from GenericBehaviour. This class corresponds to basic walk and run behaviour, it is the default behaviour.
public class Hannamove: MonoBehaviour
{
public float speednow = 1.0f;
public float walkSpeed = 2.0f; // Default sprint speed.
public float runSpeed = 2.0f; // Default sprint speed.
public string jumpButton = "Jump"; // Default jump button.
public float jumpnow = 10f;
public float jumpforce = 10f; // Default horizontal inertial force when jumping.
public float gravityyo = 10f;
private Vector3 dir = Vector3.zero;
//private
public bool jump; // Boolean to determine whether or not the player started a jump.
public bool isColliding; // Boolean to determine if the player has collided with an obstacle.
public Rigidbody playerrig;
// Start is always called after any Awake functions.
void Start(){
}
// Update is used to set features regardless the active behaviour.
//////////////////////
public void MoveInDirectionOfInput() {
//CharacterController controller = GetComponent<CharacterController>();
dir.x = Input.GetAxis("Horizontal");
dir.z = Input.GetAxis("Vertical");
///
Vector3 camDirection = Camera.main.transform.rotation * dir;
Vector3 targetDirection = new Vector3(camDirection.x, 0f, camDirection.z);
if (dir != Vector3.zero) {
transform.rotation = Quaternion.Slerp(
transform.rotation,
Quaternion.LookRotation(targetDirection),
Time.deltaTime * speednow);
///
}
/////////////////////////
if (!jump && Input.GetButtonDown(jumpButton)) {
jump = true;
jumpnow = jumpforce;
targetDirection.y = jumpforce;
}
//////////////
///////////jumpforce gravityyo
if (isColliding){
playerrig.velocity = targetDirection.normalized * speednow;
}
///
}
/////////////////////
void Update(){
MoveInDirectionOfInput();
}
/////////////////////////
// Collision detection.
private void OnCollisionStay(Collision collision){
isColliding = true;
jump = false;
}
private void OnCollisionExit(Collision collision){
isColliding = false;
//GetComponent<CapsuleCollider>().material.dynamicFriction = 0.6f;
//GetComponent<CapsuleCollider>().material.staticFriction = 0.6f;
}
}
Answer by DCordoba · Oct 27, 2020 at 09:51 AM
The problem is that you are setting playerrig.velocity
on line 42 before you did the jump mechanic.
You need to set the result force after finish the calculations so move all line 42 at the end of MoveInDirectionOfInput()
method.
This will add a small bug, the character can now move while is jumping, to solve this put a if
to ingnore the axis input when isColliding
is false
I fix falling and moving. It`s cool. But i don`t understand your end about "to solve this put a if to ingnore the axis input when isColliding is false" and how fix jumping? In code reference if you can.
line 28, wrap the dir
variable assign with a if(isColliding) to avoid read the axis input when you are in the middle of air
some games, like old mario, you can still move the character when you are jumping/falling, is part of the gameplay, but this feels unrealistic in more "serious" games.
if (isColliding){
dir.x = Input.GetAxis("Horizontal");
dir.z = Input.GetAxis("Vertical");
}
All good but Player can`t jump
using UnityEngine;
// $$anonymous$$oveBehaviour inherits from GenericBehaviour. This class corresponds to basic walk and run behaviour, it is the default behaviour.
public class Hannamove: $$anonymous$$onoBehaviour
{
public float speednow = 1.0f;
public float walkSpeed = 2.0f; // Default sprint speed.
public float runSpeed = 2.0f; // Default sprint speed.
public string jumpButton = "Jump"; // Default jump button.
public float jumpnow = 10f;
public float jumpforce = 10f; // Default horizontal inertial force when jumping.
public float gravityyo = 10f;
private Vector3 dir = Vector3.zero;
//private
public bool jump; // Boolean to deter$$anonymous$$e whether or not the player started a jump.
public bool isColliding; // Boolean to deter$$anonymous$$e if the player has collided with an obstacle.
public Rigidbody playerrig;
// Start is always called after any Awake functions.
void Start(){
}
// Update is used to set features regardless the active behaviour.
//////////////////////
public void $$anonymous$$oveInDirectionOfInput() {
//CharacterController controller = GetComponent<CharacterController>();
if (isColliding){
dir.x = Input.GetAxis("Horizontal");
dir.z = Input.GetAxis("Vertical");
}
Vector3 camDirection = Camera.main.transform.rotation * dir;
Vector3 targetDirection = new Vector3(camDirection.x, 0f, camDirection.z);
if (dir != Vector3.zero) {
transform.rotation = Quaternion.Slerp(
transform.rotation,
Quaternion.LookRotation(targetDirection),
Time.deltaTime * speednow);
///
}
if (isColliding && Input.GetButtonDown(jumpButton)) {
jump = true;
jumpnow = jumpforce;
targetDirection.y = jumpforce;
}
///
targetDirection.y -= jumpforce * Time.deltaTime;
///////////jumpforce gravityyo
if (isColliding){
playerrig.velocity = targetDirection.normalized * speednow;
}
///
}
/////////////////////
void Update(){
$$anonymous$$oveInDirectionOfInput();
}
/////////////////////////
// Collision detection.
private void OnCollisionStay(Collision collision)
{
isColliding = true;
jump = false;
}
private void OnCollisionExit(Collision collision)
{
isColliding = false;
//GetComponent<CapsuleCollider>().material.dynamicFriction = 0.6f;
//GetComponent<CapsuleCollider>().material.staticFriction = 0.6f;
}
}
ahhh, remove if (isColliding)
wrapper form the last one, you need to update this one even on the middle of air...
change
if (isColliding){
playerrig.velocity = targetDirection.normalized * speednow;
}
just by
playerrig.velocity = targetDirection.normalized * speednow;
Your answer
Follow this Question
Related Questions
zombies and the player SUDDENLY(not a beginning but a middle of playing) falling down 1 Answer
Adding falling and landing animations to the Locomotion System 1 Answer
falling through the terrain 0 Answers
Endless "Falling in place" Simulation Question 0 Answers
Adding Jumping and Falling Animations 0 Answers