I created a move script with jumping. But when i jump it doesn’t add the momentum from moving in a direction and want to add this affect. Especially when running.
using UnityEngine;
using System.Collections;
public class PlayerMove : MonoBehaviour {
public float MoveSpeed;//speed of movement
public float jump_Force;// how much force is applied to jump
public float air_resistance;//how much air resistance is present
public bool can_Double_Jump;//jump twice
public bool can_hover;//slowly desend in air
public bool isGrounded;//is on the ground
public bool Parachute = false;//parachute
public bool isrunning;
// Update is called once per frame
void Update () {
OpenParachute();
Run();
if (isGrounded)
{
if (Input.GetKey(KeyCode.W))
{
rigidbody.velocity = MoveSpeed * Vector3.forward;
}
else if (Input.GetKey(KeyCode.S))
{
rigidbody.velocity = MoveSpeed * Vector3.back;
}
if (Input.GetKey(KeyCode.A))
{
rigidbody.velocity = MoveSpeed * Vector3.left;
}
else if (Input.GetKey(KeyCode.D))
{
rigidbody.velocity = MoveSpeed * Vector3.right;
}
if (Input.GetButtonDown("Jump"))//can execute first jump
{
rigidbody.velocity = jump_Force * Vector3.up;//adds velocity upward
isGrounded = false;//is not on ground
can_Double_Jump = true;//can jump again
}
}
else if (Input.GetButtonDown("Jump") && can_Double_Jump)// can jump again once first jump is done
{
rigidbody.velocity = new Vector3(0, jump_Force, 0);//adds a velocity upward
can_Double_Jump = false;//cant jump again
can_hover = true;// can now hover if appropiate button is pressed
}
else if (Input.GetKey(KeyCode.LeftControl) && can_hover)//opens the parachute while falling
{
Parachute = true;//parachute is open
can_hover = false;//cant open parachute again
}
if (Input.GetKeyUp(KeyCode.LeftControl))
{
Parachute = false;//releases parachute
}
}
void OnCollisionEnter(Collision other)
{
if (other.gameObject.tag == "Ground")
{
isGrounded = true;//is on the ground
Parachute = false;//parachute is not open
}
}
void OpenParachute()//open parachute to decend slowly due to drag
{
if (Parachute)//is parachute open
{
rigidbody.drag = 10;//set air drag to 10
}
else
{
rigidbody.drag = .2f;//set air drag to .2
}
}
void Run()
{
if (isrunning)
{
MoveSpeed = 10;//move speed increases
}
else
{
MoveSpeed = 4;
}
if (Input.GetKey(KeyCode.LeftShift))
{
isrunning = true;
}
if (Input.GetKeyUp(KeyCode.LeftShift))
{
isrunning = false;
}
}
}