- Home /
can anyone help me to smooth the jump?
hello guys i need a little help i have a player jump correctly when i'm not moving ..but when i'm running if i press the space bar and release it when my player is in air , i noticed that the player makes a double charged jump and i don't want that, i want to make single jump using UnityEngine; using System.Collections;
public class player_motion123 : MonoBehaviour {
public Animator anim;
//public Transform center_point;
public Quaternion newrotation;
public float smooth = 0.05f;
public float speed;
public CharacterController controller;
public static bool ismoving=false;
public static bool isRunning=false;
public Transform cam;
bool grounded;
// jump
private float verticalVelocity;
public float gravity;
public float JumpForce;
public float JumpForce2;
public bool jumping;
void Start()
{
}
void Update ()
{
float v = Input.GetAxis ("Vertical");
float h = Input.GetAxis ("Horizontal");
if (player_Sword_Attack.attacking == false)
{
Move (h, v);
Run (h, v);
jump ();
}
}
void Move(float h ,float v)
{
if (h != 0f || v != 0f)
{
rotate (v, h);
controller.SimpleMove (transform.forward * speed);
anim.SetFloat ("Speed", 0.5f);
ismoving = true;
} else {
anim.SetFloat ("Speed",0);
ismoving = false;
}
}
void Run(float h, float v)
{
if (ismoving && Input.GetKey (KeyCode.LeftShift)) {
rotate (v, h);
controller.SimpleMove (transform.forward * speed * 2);
anim.SetFloat ("Speed", 1);
isRunning = true;
}
else if (ismoving && Input.GetKeyUp (KeyCode.LeftShift)) {
anim.SetFloat ("Speed", 0.5f);
isRunning = false;
}
else if (!ismoving && Input.GetKeyUp (KeyCode.LeftShift)) {
anim.SetFloat ("Speed", 0);
isRunning = false;
}
}
void jump()
{
if (controller.isGrounded)
{
verticalVelocity = -gravity * Time.deltaTime;
anim.SetBool ("Jumping",false);
if (Input.GetKeyDown (KeyCode.Space))
{
verticalVelocity = JumpForce;
jumping = true;
if (isRunning)
verticalVelocity = JumpForce2;
jumping = true;
}
}
else
{
verticalVelocity -= gravity * Time.deltaTime;
if(Input.GetKeyUp(KeyCode.Space))
anim.SetBool ("Jumping",true);
jumping = false;
}
Vector3 moveVector = Vector3.zero;
moveVector.x = Input.GetAxis ("Horizontal");
moveVector.y = verticalVelocity;
moveVector.z = Input.GetAxis("Vertical")*5.0f;
controller.Move (moveVector*Time.deltaTime);
}
void rotate(float v,float h) {
if (v > 0)
{
if (h > 0)
{
newrotation = Quaternion.Euler(0,cam.eulerAngles.y+45,0);
}
else if (h < 0)
{
newrotation = Quaternion.Euler(0,cam.eulerAngles.y+305,0);
}
else
{
newrotation = Quaternion.Euler(0,cam.eulerAngles.y,0);
}
}
else if (v < 0)
{
if (h > 0)
{
newrotation = Quaternion.Euler(0,cam.eulerAngles.y+135,0);
}
else if (h < 0)
{
newrotation = Quaternion.Euler(0,cam.eulerAngles.y+225,0);
}
else {
newrotation = Quaternion.Euler(0,cam.eulerAngles.y+180,0);
}
}
else
{
if (h > 0)
{
newrotation = Quaternion.Euler(0,cam.eulerAngles.y+90,0);
}
else if (h < 0)
{
newrotation = Quaternion.Euler(0,cam.eulerAngles.y+270,0);
}
else {
newrotation = transform.rotation;
}
}
newrotation.x = 0;
newrotation.z = 0;
//We only want player to rotate in y axis
transform.rotation = Quaternion.Slerp (transform.rotation,newrotation, smooth);
//Slerp from player's current rotation to the new intended rotaion smoothly
}
}
if i'm running and i jump and still press the W key it works fine,,,, but try it when running and jump and quickly remove your hand from W and shiftkey when the player is in the air it will make double jump
Your answer
Follow this Question
Related Questions
jump with character controller best way? 1 Answer
How do I make this jump function work? 1 Answer
Not able to jump 1 Answer
Can't figure out how to detect ground (2D) 1 Answer
Using isGrounded with a RigidBody 2 Answers