- Home /
Moving forward whilst jumping
I'm working on a game where the player is constantly running (third person) and has the ability to jump. With my script at the moment, when the player jumps (achieved by an upwards swipe on android in another script), the player stops moving forward and jumps, then returns to moving forward. I want it so that he jumps and continues but everything I try seems to not work. Any ideas? I've added some of the code with irrelevant parts removed. Thank you.
function Update() {
var controller : CharacterController = GetComponent(CharacterController);
if (controller.isGrounded && deadPlayer == false) {
// We are grounded, so recalculate
// move direction directly from axes to keep right direction after turn
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
isMoving = true;
var MoveDirection = transform.forward;
controller.Move(MoveDirection * Time.deltaTime * speed);
// Tilt controls
//direction.x = -Input.acceleration.y * moveSpeed;
direction.x = Input.acceleration.x * moveSpeed;
if (direction.sqrMagnitude > 1)
direction.Normalize();
// Make it move 10 meters per second
direction *= Time.deltaTime;
direction = Camera.main.transform.TransformDirection(direction);
// Move object without transform to take boundaries into consideration
controller.Move (direction * speed);
if (jump == true) {
moveDirection.y = jumpSpeed;
camera1.audio.PlayOneShot(jumpSound);
isJumping = true;
jump = false;
}
else
{
isJumping = false;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
You do realise that your only coding it to moving forward when controller.isGrounded
? That should give you enough of a hit of how to change it :)
Just reading through the comments in the code gives you the answer
Thanks for the response. I've tried removing the isGrounded condition, which then jumps and moves forward but the player remains in the air for a long time. It's as if gravity wasn't effecting correctly. How can I fix this?
The problem is probably because you are calling $$anonymous$$ove()
more than once. You should never do this, ever
Just looking at the docs should give you the answer, but I'll post some code later :) http://docs.unity3d.com/Documentation/ScriptReference/CharacterController.$$anonymous$$ove.html
Answer by el_rolas · Apr 06, 2013 at 04:19 AM
moveDirection.y += jumpSpeed;
2 things wrong with this answer.
It's wrong, it will yield a different result every time you jump
It's not an answer to the question. Yes that is how you jump, but as you can see, it's already in the code (the correct version).
Ergo you have given a wrong solution to something that wasn't a problem
Thank you
Answer by Benproductions1 · Apr 03, 2013 at 03:23 AM
Hello
Here is the fixed up code:
//Firstly move the controller variable outside of the update loop (optimisation)
var controller:CharacterController;
var moveDirection:Vector3;
function Awake() {
//Only once find the character controller using the fastest method
controller = GetComponent(typeof(CharacterController)) as CharacterController;
}
function Update() {
//I removed a large amount of code, since it seemed it was never used
//The first thing we should do is calculate the local direction we should move in
if (deadPlayer == false) {
//We arent dead, so we can move
//Calculate the movement speed according to accelerometer
moveDirection.x = Input.acceleration.x*speed;
//Now the constant moving forward
moveDirection.z = speed;
/*If you want to be able to move with the axis as well, just un-comment this:
moveDirection.x += Input.GetAxis("Horizontal")*speed;
*/
//I didn't change your jumping
if (jump == true && controller.isGrounded) {
//Only Jump when grounded
moveDirection.y = jumpSpeed;
camera1.audio.PlayOneShot(jumpSound);
isJumping = true;
jump = false;
}
else {
isJumping = false;
}
}
//Now we move, and only once
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(Camera.main.transform.TransformDirection(moveDirection) * Time.deltaTime);
}
I believe this is what you are looking for. Any problems? Don't hesitate to ask
Benproductions1
Thank you very much. As the part to continually move forward is gone, won't the player remain stationary now? I'll give it a try.
I've just tried it and the jump works fine now (the player comes straight back down to the ground), but now as feared the player will stay in the same spot without moving forward. How can I apply this without having to call $$anonymous$$ove again?
I've tried adding in a few lines to constantly move forward, but the player no longer takes direction into consideration (when the player turns 90 degrees, he continues moving in the same direction). Any ideas? Thanks.
right now direction is relative to the camera, if yyou want it relative to the player make the nessesary changes to line 40
Answer by nextage575 · Dec 12, 2019 at 06:46 PM
@Benproductions1 i need controller something like Character should move forward and jump parabolic continually ,and right life on swiping left or right how can i achieve it,?
Your answer
Follow this Question
Related Questions
Jumping with Character Controller?! 1 Answer
creating 2d side scroller for android cant get my player to jump help skl project 0 Answers
How do I script so my character Jumps farther when Running? 1 Answer
Player Character looses jump height the further right they go 1 Answer
Jumping effectively in a 2D sidescroller 0 Answers