Question by
IamUnityAddict · May 10, 2017 at 11:13 PM ·
unity 5jumplong
how to make a character a long jump in unity ?
using UnityEngine; using System.Collections;
public class PlayerControl : MonoBehaviour {
public float moveSpeed;
private float moveSpeedStore;
public float jumpForce;
public float jumpTime;
private float jumpTimeCounter;
public float speedMutiplayer;
public float speedIncreaseMilestone;
private float speedIncreaseMilestoneStore;
private float speedMilestoneCount;
private float speedMilestoneCountStore;
private Rigidbody2D myRigibody;
public bool grounded;
public LayerMask whatIsGrounded;
public Transform groundCheck;
public float groundCheckRadius;
public GameManager theGameManager;
private bool stoppedJumping;
private bool canDoubleJump;
private Animator myAnimator;
float deltaTime = 0.0f;
public AudioSource DeathSound;
// Use this for initialization
void Start () {
myRigibody = GetComponent<Rigidbody2D>();
myAnimator = GetComponent<Animator>();
jumpTimeCounter = jumpTime;
speedMilestoneCount = speedIncreaseMilestone;
moveSpeedStore = moveSpeed;
speedMilestoneCountStore = speedMilestoneCount;
speedIncreaseMilestoneStore = speedIncreaseMilestone;
stoppedJumping = true;
}
// Update is called once per frame
void Update () {
UptCode();
}
void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.tag == "killbox")
{
theGameManager.RestartGame();
moveSpeed = moveSpeedStore;
speedMilestoneCount = speedMilestoneCountStore;
speedIncreaseMilestone = speedIncreaseMilestoneStore;
DeathSound.Play();
}
}
void OnGUI()
{
int w = Screen.width, h = Screen.height;
GUIStyle style = new GUIStyle();
Rect rect = new Rect(0, 0, w, h * 2 / 100);
style.alignment = TextAnchor.UpperLeft;
style.fontSize = h * 2 / 100;
style.normal.textColor = new Color(0.0f, 0.0f, 0.5f, 1.0f);
float msec = deltaTime * 1000.0f;
float fps = 1.0f / deltaTime;
string text = string.Format("{0:0.0} ms ({1:0.} fps)", msec, fps);
GUI.Label(rect, text, style);
}
public void UptCode()
{
deltaTime += (Time.deltaTime - deltaTime) * 0.1f;
if (Input.GetKey(KeyCode.Escape))
{
Application.LoadLevel("Menu");
}
grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGrounded);
if (transform.position.x > speedMilestoneCount)
{
speedMilestoneCount += speedIncreaseMilestone;
speedIncreaseMilestone = speedIncreaseMilestone * speedMutiplayer;
moveSpeed = moveSpeed * speedMutiplayer;
}
myRigibody.velocity = new Vector2(moveSpeed, myRigibody.velocity.y);
if (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0))
{
if (grounded)
{
myRigibody.velocity = new Vector2(myRigibody.velocity.x, jumpForce);
stoppedJumping = false;
}
if (!grounded && canDoubleJump)
{
myRigibody.velocity = new Vector2(myRigibody.velocity.x, jumpForce);
jumpTimeCounter = jumpTime;
stoppedJumping = false;
canDoubleJump = false;
}
}
if ((Input.GetKey(KeyCode.Space) || Input.GetMouseButton(0)) && !stoppedJumping)
{
if (jumpTimeCounter > 0)
{
myRigibody.velocity = new Vector2(myRigibody.velocity.x, jumpForce);
jumpTime -= Time.deltaTime;
}
}
if (Input.GetKeyUp(KeyCode.Space) || Input.GetMouseButtonUp(0))
{
jumpTimeCounter = 0;
stoppedJumping = true;
}
if (grounded)
{
jumpTimeCounter = jumpTime;
stoppedJumping = true;
canDoubleJump = true;
}
if (!grounded)
{
jumpTime = 0;
}
myAnimator.SetFloat("Speed", myRigibody.velocity.x);
myAnimator.SetBool("Grounded", grounded);
}
}
Comment