- Home /
This question was
closed Nov 14, 2013 at 01:46 AM by
rutter for the following reason:
Duplicate of http://answers.unity3d.com/questions/575099/multiple-jump-c-script-1.html
Question by
Breckenridge119 · Nov 14, 2013 at 01:43 AM ·
c#jumpjumpingdoublejump
Multiple jump C# Script.
I am attempting to remake the game of Joust. For it I need to be able to jump more than once and when in the air. If someone could assists me that would be fantastic. Here is the code I have so far.
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(PlayerPhysics))]
public class PlayerController : MonoBehaviour {
// Player Handling
public float gravity = 20;
public float speed = 8;
public float acceleration = 30;
public float jumpHeight = 12;
private float currentSpeed;
private float targetSpeed;
private Vector2 amountToMove;
private PlayerPhysics playerPhysics;
void Start () {
playerPhysics = GetComponent<PlayerPhysics>();
}
void Update () {
targetSpeed = Input.GetAxisRaw("Horizontal") * speed;
currentSpeed = IncrementTowards(currentSpeed, targetSpeed,acceleration);
if (playerPhysics.grounded) {
amountToMove.y = 0;
// Jump
if (Input.GetButtonDown("Jump")) {
amountToMove.y = jumpHeight;
}
}
amountToMove.x = currentSpeed;
amountToMove.y -= gravity * Time.deltaTime;
playerPhysics.Move(amountToMove * Time.deltaTime);
}
// Increase n towards target by speed
private float IncrementTowards(float n, float target, float a) {
if (n == target) {
return n;
}
else {
float dir = Mathf.Sign(target - n); // must n be increased or decreased to get closer to target
n += a * Time.deltaTime * dir;
return (dir == Mathf.Sign(target-n))? n: target; // if n has now passed target then return target, otherwise return n
}
}
}
Comment
Follow this Question
Related Questions
Multiple jump C# Script. 2 Answers
Triple Jump C# Script 1 Answer
C# Movement Script 0 Answers
Cant Jump ?c# please 2 Answers
how to jump with 2d? 1 Answer