- Home /
Particular "Jump" Script.
Hello! I have a problem with my script, I have not found anything that can help me that has already been asked. This is my script jump, I would like it if my finger is on the button the player starts to rise high, and when I release it back down, Like Jetpack JoyRide.
Any Ideas?
function Update()
{
var movement = Vector3.zero;
MainCharacter.animation.CrossFade ("Run",2);
// Apply movement from move joystick
if ( moveTouchPad.position.x > 0 )
movement = Vector3.right * forwardSpeed * moveTouchPad.position.x;
else
movement = Vector3.right * backwardSpeed * moveTouchPad.position.x;
// Check for jump
if ( character.isGrounded )
{
var jump = false;
var touchPad = jumpTouchPad;
if ( !touchPad.IsFingerDown() )
canJump = true;
if ( canJump && touchPad.IsFingerDown() )
{
jump = true;
canJump = false;
}
if ( jump )
{
// Apply the current movement to launch velocity
velocity = character.velocity;
velocity.y = jumpSpeed;
}
}
else
{
// Apply gravity to our velocity to diminish it over time
velocity.y += Physics.gravity.y * Time.deltaTime;
// Adjust additional movement while in-air
MainCharacter.animation.CrossFade ("Jump");
movement.x *= inAirMultiplier;
// movement.z *= inAirMultiplier;
}
movement += velocity;
movement += Physics.gravity;
movement *= Time.deltaTime;
// Actually move the character
character.Move( movement );
if ( character.isGrounded )
// Remove any persistent velocity after landing
velocity = Vector3.zero;
}
Answer by robertbu · Oct 05, 2013 at 04:12 PM
I can tell you why you are having a problem, but I it will take some work on your part to figure out a fix. Right now you set the velocity for a jump on line 31. This is inside of if 'character.isGrounded', which means that once the jump is started, that line is not executed. You need one of two things to happen. Either you need to have the 'y' velocity set as long as the finger is held on the touchPad, or you need to suspend gravity while the finger is held on the touchPad. I would try first replacing line 47 with:
if (!jumpTouchPad.IsFingerDown()) {
movement += Physics.gravity;
}
I suspect there will be other changes, but this is a possible first step.
Answer by YoungDeveloper · Oct 05, 2013 at 05:07 AM
Create a button. On button press increase transform.localPosition.y plus some float * Time.deltaTime. You should also have two variables which will represent maxheight and ground base position, so you know much can you lower or higher the character. You lower the character same way you higher it but with minus.
i try this but isn't work.
player.transform.localPosition.y += force * Time.deltaTime;
Did you even did some research before asking or you just tried to write what i said ? You have to understand what exactly you want to move, if that object is parented or not, that is should you use localpos.. or just position.
Something like this, idea itself.
float maxHeight;
void Start(){
maxHeight = transform.position.y;
maxHeight += 5f;
}
Update(){
//if button pressed{
Vector3 currHeight = transform.position;
currHeight.y += 1f * Time.deltaTime;
if(currHeight.y > maxHeight) currHeight.y = maxHeight;
transform.position = currHeight;
}
}