- Home /
Trying to make a doublejump script, problems using rigidbody
So I'm new to unity and I have been researching double jump for a while. It's apparently difficult... at least it is for me... So I have set up a custom Player Controller script, a rigid body, and a Character Controller (which I don't think I am even using anymore...). I am not brand new to JS but I am not exactly a seasoned veteran when it comes to actually using it as a scripting language outside of HTML... What I am trying to do is make a simple double jumping character by following other people's scripts and tutorials, but I am trying to do it my own way, a simpler way, and in general, a different way from which I have seen done. I thought it would be possible to do this with my current setup without having to forfeit what I have already done - however through my struggles I have redone basically everything from scratch anyways...
I digress...
Here is the code for my Script that is supposed to move the Character left and right and allow for double jump if the character is able. Can someone please tell me what I am doing wrong? This is mostly a learning experience. I don't learn well from Youtube tutorials, so If someone could help me understand what I am doing that would be great!
var autoRotate : boolean = true;
var maxRotationSpeed : float = 360;
var airJumpMax : int = 1;
var jumpForce : float = 8;
var airJumpForce : float = jumpForce * 0.6;
var walkSpeed : float = 8;
private var xMove : int = 0;
private var airJumpAmt : int = 0;
private var isGrounded : boolean = false;
function Awake () {
motor = GetComponent(CharacterMotor);
}
function Update () {
//Double jump raycasting
if(Physics.Raycast(transform.position, -Vector3.up, 1)) {
isGrounded = true;
airJumpAmt = airJumpMax;
}
if(Input.GetKeyDown(KeyCode.Space)){
if(isGrounded) {
JumpNormal();
}else if(airJumpAmt > 0) {
airJumpAmt -= 1;
JumpInAir();
}
}
xMove = 0;
xMove = Input.GetAxisRaw("Horizontal") * walkSpeed;
}
function FixedUpdate(){
rigidbody.velocity = new Vector3(xMove*walkSpeed,0,0);
}
function projectOntoPlane (v : Vector3, normal : Vector3) {
return v - Vector3.Project(v, normal);
}
function JumpNormal () { rigidbody.AddForce(Vector3.up * jumpForce, ForceMode.Impulse); }
function JumpInAir () { rigidbody.AddForce(Vector3.up * airJumpForce, ForceMode.Impulse); }
function constantSlerp (from : Vector3, to : Vector3, angle : float) {
var value : float = Mathf.Min(1, angle / Vector3.Angle(from, to));
return Vector3.Slerp(from, to, value);
}
// Require a character controller to be attached to the same game object
@script AddComponentMenu ("Character/Platform Input Controller");
EDIT: So I had to change some original values a little bit but eventually yes my double jump now works. However I am still having problems moving horizontally. The character, currently, slowly drags to the ground because of this
function FixedUpdate(){ rigidbody.velocity = new Vector3(xMove*walkSpeed,0,0); }
I am not sure how to move my player horizontally, while keeping vertical and horizontal momentum, using my current setup... I do have a character controller in the player too, not just my script, but I haven't used it because... Well... I don't know what it does or how to use it. Basically this script causes the character to slowly, and choppily, sink to the ground from starting position. It cannot jump and cannot move. That is because of what is in the "FixedUpdate" function. I fixed the double jump but the character can't move left or right without totally spazzing out.
Why do you say you are doing wrong? What does your code do? What do you expect it to do? $$anonymous$$nowing that could help everyone in trying to help you.
It seems to me that you could have some trouble with the logic used to check if the character is grounded. Ins$$anonymous$$d of using a raycast for the double jump, why not use a state machine (just for the jump)? If state equals grounded call JumpNormal, else if state equals jumpingNormal call JumpInAir.
JumpNormal function sets state to jumpingNormal and going back to the ground sets it to grounded.
Yea I do not actually know how to use a state machine or whatever you are talking about... And I'll edit the post with your suggestions thanks!
You could try function FixedUpdate() { rigidbody.velocity.x = x$$anonymous$$ove * walkSpeed; }
ins$$anonymous$$d of what you have, as right now you're setting vertical velocity to zero at the beginning of every physics update.
Answer by indeed005 · Jun 18, 2014 at 06:51 AM
Is your first jump propelling your character more than 1 unit above the ground?
I ask because you are raycasting 1 unit down. If not, then this clause will never be reached:
}else if(airJumpAmt > 0) {
airJumpAmt -= 1;
JumpInAir();
}
Your jumpforce is 8, but I don't know how heavy your rigidbody is, or if you are using normal gravity. Put a breakpoint on it and find out
Your answer
Follow this Question
Related Questions
Any tips to fix and improve this movement script? 0 Answers
Double jump does not work why? 0 Answers
Instantiating two rigidbody at a time 0 Answers
Making 3D Objects Stick On Other Objects Like A Grid? 2 Answers
Why is my object not moving 1 Answer