- Home /
Problems with jumping script
I have tried to create an alternative to the jumping section of the Character Motor to allow the player, no matter where I turn the gravity, to jump in opposite direction. But I encounter a problem with the basic element of jumping, the character sometimes refuses to jump, instead wriggling a little on the ground. Sometimes he shakes, and then jumps. I am not sure what causes this, as I have tried to use the most basic function.
I have reduced the code to the very essence of what should make the character jump, so the problem is definitely somewhere here. Does anybody know what stupid mistake I made this time?
using UnityEngine;
using System.Collections;
public class Player_Jump_Script_New : MonoBehaviour
{
public Vector3 moveDirection;
public CharacterController Controller;
public float gravity;
void Start ()
{Controller = GetComponent<CharacterController>();}
void Update ()
{
if(CanJump==true)
{
if (Input.GetButtonDown("Jump"))
{
CanJump=false;
moveDirection.y = jumpSpeed;
StartCoroutine(JumpOver(0.5f));
}
Controller.Move(moveDirection * Time.deltaTime);
}
}
IEnumerator JumpOver (float temp)
{
yield return new WaitForSeconds (temp);
if(IsJumping==true)
{
moveDirection.y = -gravity;
CanJump=true;
}
}
}
Answer by Makiavel · Feb 26, 2014 at 08:37 AM
Forger it, I have created a normal rigidbody character movement system, so this isn't a problem anymore)
if anybody will find this answer searching to solve a similar problem, here's my solution:
using UnityEngine;
using System.Collections;
public class RigidbodyJump : MonoBehaviour {
public bool CanJump;
public float Player_Gravity;
public float MinJumpHeight;
public float force;
public bool IsJumping;
public bool CheckIfCanJump;
public float speed;
public float WalkingSpeed;
public float RunningSpeed;
public float RunningTimer;
public float RunningTimerMax;
public float RunningTimerDrain;
public float RunningTimerRecharge;
private bool IsRunning;
public Vector3 MovementDirection;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void FixedUpdate ()
{
if(Input.GetKey("left shift")&&RunningTimer>0)
{
IsRunning=true;
}
else
{
IsRunning=false;
}
if(IsRunning==true)
{
speed = RunningSpeed;
RunningTimer -= RunningTimerDrain;
}
else
{
speed = WalkingSpeed;
if(RunningTimer<=RunningTimerMax)
{
RunningTimer += RunningTimerRecharge;
}
}
if(Input.GetButtonDown("Jump"))
{
if(CanJump==true)
{
transform.rigidbody.AddForce(Vector3.up * force);
IsJumping=true;
CanJump=false;
StartCoroutine(CheckIfCanJumpAgain(0.1f));
}
}
if(CheckIfCanJump==true)
{
RaycastHit hit;
if (Physics.Raycast(transform.position, -transform.up, out hit, 100.0F))
{
if(hit.distance<=MinJumpHeight)
{
CanJump=true;
IsJumping=false;
CheckIfCanJump=false;
}
}
}
if(Input.GetButton("w"))
{
MovementDirection.z = 1;
}
else
{
MovementDirection.z = 0;
}
if(Input.GetButton("s"))
{
MovementDirection.z = -1;
}
if(Input.GetButton("a"))
{
MovementDirection.x = -1;
}
else
{
MovementDirection.x = 0;
}
if(Input.GetButton("d"))
{
MovementDirection.x = 1;
}
transform.rigidbody.AddForce(-Vector3.up * Player_Gravity);
transform.Translate(MovementDirection * speed);
}
IEnumerator CheckIfCanJumpAgain (float temp)
{
yield return new WaitForSeconds (temp);
CheckIfCanJump = true;
}
public void SwitchGravity()
{
Player_Gravity = -Player_Gravity;
}
}
Your answer
Follow this Question
Related Questions
Jumping script WONT WORK!!!!!!!! for 2D 1 Answer
Need help with making a character jump. 1 Answer
How do you make a characterController jump automatically when it reaches the edge of a platform? 0 Answers
jump with character controller best way? 1 Answer
How do I fix my player jumping code? 2 Answers