- Home /
Help with my double jump script
I am trying to get my character to jump in mid air either left right or just straight up depending on which key I am pressing and also need it to ignore my current velocity however I have not been able to achieve this. I have tried everything from making a doublejump function with else if statements to consulting ancient gypsy fortune tellers but have not been able to get my desired result. Heres my script:
var speed : float = 4.0;
var jump : float = 10.0;
var dJump : float = 10.0;
var gravity : float = 20.0;
var dJumpV : float = -4.0;
var dJumpV0 : float = 4.0;
private var canDJump : boolean = false;
private var velocity : Vector3 = Vector3.zero;
var controller : CharacterController;
function Update()
{
if (!controller.isGrounded && canDJump && Input.GetKey("a")
`|| !controller.isGrounded && canDJump && Input.GetKey("left")) { ldJump(); } if (!controller.isGrounded && canDJump && Input.GetKey("d")
|| !controller.isGrounded && canDJump && Input.GetKey("right")) { rdJump(); }
if (!controller.isGrounded && canDJump && Input.GetButtonDown("Jump"))
{
velocity.y = dJump;
canDJump = false;
}
if (controller.isGrounded)
{
velocity = Vector3 (Input.GetAxisRaw ("Horizontal"), 0, 0);
velocity = transform.TransformDirection (velocity);
velocity *= speed;
if (Input.GetButtonDown ("Jump"))
{
velocity.y = jump;
canDJump = true;
}
}
velocity.y -= gravity * Time.deltaTime;
controller.Move (velocity * Time.deltaTime);
}
function ldJump()
{
if (Input.GetButtonDown ("Jump"))
{
velocity.y = jump;
velocity.x = dJumpV;
}
}
function rdJump()
{
if (Input.GetButtonDown ("Jump"))
{
velocity.y = jump;
velocity.x = dJumpV0;
}
}
your code paste is all jacked. You'll need to clean that up before anyone looks at this.
So I figured it out sorta except that I can't jump straight up if my first jump was to either side hopefully someone can help me with that part. Anyway there's the code that works for me if anybody needs it.
it can't get any cleaner than that at least not from me.
Answer by Mr-JWolf809 · Jun 04, 2013 at 12:14 AM
Heya,
I think you have complicated the double jump a bit too much. You can use the same approachas you used in your normal jump to figure out the Horizontal movement.
Try this:
#pragma strict
var speed : float = 4.0; var jump : float = 10.0; var dJump : float = 10.0; var gravity : float = 20.0; var dJumpV : float = 4.0; private var canDJump : boolean = false; private var velocity : Vector3 = Vector3.zero; var controller : CharacterController;
function Update() {
if (!controller.isGrounded && canDJump && Input.GetButtonDown ("Jump"))
{
velocity = GetHorizontalMovementDirection();
velocity *= dJumpV;
velocity.y = dJump;
canDJump = false;
}
if (controller.isGrounded)
{
velocity = GetHorizontalMovementDirection();
velocity *= speed;
if (Input.GetButtonDown ("Jump"))
{
velocity.y = jump;
canDJump = true;
}
}
velocity.y -= gravity * Time.deltaTime;
controller.Move (velocity * Time.deltaTime);
}
function GetHorizontalMovementDirection() { var direction : Vector3 = Vector3 (Input.GetAxisRaw ("Horizontal"), 0, 0); return transform.TransformDirection (direction); }
Wow worked like a charm thank you. Time for me to look over your code and figure out why it works. I guess what has me stuck is line 19 in your script. why is velocity *= dJumpV only affecting the X axis?
How would you make this so you can move left or right in mid air?
@TheRealOsme wow you're lucky I suddenly decided to look at my old questions activity. The reason you can't move in the air is because the controller needs to be grounded in order to be able to move horizontally. If you eraser line 24 along with 25 and 33 which have the {}that belong to that if statement, you should be able to move while jumping. Haha when I first saw this code it was so confusing, now it looks so simple.
when I remove lines 24 along with 25 and 33 i get the error "UnassignedRefrenceException: The variable controller of "CharacterController' has no been assigned"
I'm on the wiiU right now, I'll get on the pc later and help you get the code working. However, it sounds like you haven't assigned the character controller to the controller variable in the inspector. You may have reset the script or assigned the script to a new object. $$anonymous$$ake sure you drag the character controller into the controller variable. If this confuses you let me know, I'll gladly answer any questions you might have
Your answer
Follow this Question
Related Questions
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Translation of an object 2 Answers
AddForce to sphere 1 Answer