C# keep previous velocity while jumping, previous solutions not working
I'm trying to implement a simple jump and it's just not working how it should. When the player jumps whilst running, it just jumps in one spot i.e. doesn't continue moving forward. I've seen lots of solutions to this but none are working for me! I'm using the character and animations from the free RPG character pack by Explosive on the asset store however I'm coding the movement and animator myself (university assignment). I have 2 years experience with unity and c# but this is just confusing me so much. Sorry for the wall of code:
public class PlayerMovement : MonoBehaviour
{
private Quaternion deltaRotation;
private Animator animator;
private Rigidbody rb;
float input_horizontal_joystick;
float input_vertical_joystick;
float input_horizontal;
float input_vertical;
float jump;
public float sensitivity = 1.0f;
public float jump_height = 300.0f;
private string max = "maximum height";
private string ground = "grounded";
private string in_air = "in air";
private string near_ground = "near to the ground";
private float vel_x = 0;
private float vel_z = 0;
void Start ()
{
//deltaRotation = transform.rotation;
animator = GetComponent<Animator> ();
rb = GetComponent<Rigidbody> ();
}
void Inputs()
{
//Using input manager
input_horizontal_joystick = Input.GetAxis("Horizontal Joystick");
input_vertical_joystick = Input.GetAxis("Vertical Joystick");
input_horizontal = Input.GetAxis("Horizontal");
input_vertical = Input.GetAxis("Vertical");
jump = Input.GetAxis ("Jump");
}
void FixedUpdate ()
{
//Get input value every frame
Inputs ();
Debug.Log("W: " + input_vertical + " Controller: " + input_vertical_joystick + " Velocity: " + rb.velocity);
Movement ();
Jump ();
Rotation ();
}
void Movement()
{
//Forward movement
if ((input_vertical == 0) && (input_vertical_joystick == 0)
&& (input_horizontal_joystick == 0))
{
animator.SetBool("Moving", false);
}
else if (input_vertical > 0)
{
animator.SetBool("Moving", true);
animator.SetFloat("Velocity", input_vertical);
vel_x = rb.velocity.x;
vel_z = rb.velocity.z;
}
else if (input_vertical_joystick > 0)
{
animator.SetBool("Moving", true);
animator.SetFloat("Velocity", (input_vertical_joystick + Mathf.Abs(input_horizontal_joystick)));
vel_x = rb.velocity.x;
vel_z = rb.velocity.z;
}
}
void Rotation()
{
if (animator.GetBool("Moving") == true) //if moving
{
if (input_horizontal_joystick != 0)
{
deltaRotation = Quaternion.Euler(0f, (2 * (input_horizontal_joystick * sensitivity)), 0f);
rb.MoveRotation(rb.rotation * deltaRotation);
}
else if (input_horizontal != 0)
{
deltaRotation = Quaternion.Euler(0f, (2 * (input_horizontal * sensitivity)), 0f);
rb.MoveRotation(rb.rotation * deltaRotation);
}
}
}
void Jump()
{
if ((jump > 0) && (GetHeight() == ground))
{
animator.SetBool("Jumping", true);
animator.SetBool("Falling", false);
rb.AddForce(new Vector3(0, jump_height, 0), ForceMode.Impulse);
}
else if (GetHeight() == max)
{
animator.SetBool("Jumping", false);
animator.SetBool("Falling", true);
}
else if (GetHeight() == near_ground)
{
animator.SetBool("Falling", false);
}
//Tried this, doesn't work
if ((animator.GetBool("Jumping") == true) || (animator.GetBool("Falling") == true))
{
rb.velocity += new Vector3(vel_x, 0, vel_z); //tried replaing the rb.AddForce line with something similar to this, still doesnt work
}
}
string GetHeight()
{
Vector3 down = transform.TransformDirection(Vector3.down);
if (Physics.Raycast(transform.position, down, 1.8f))
{
if (Physics.Raycast(transform.position, down, 1.0f))
{
if (Physics.Raycast(transform.position, down, 0.1f))
{
return ground;
}
else
{
return near_ground;
}
}
else
{
return max;
}
}
else
{
return in_air;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Player jumping automatically 0 Answers
How can I make my player jump faster but not higher? 3 Answers
Keep gravity while adding velocity to a Rigidbody? 1 Answer
RigidBody Controller Forces 0 Answers