Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Bisonator · Apr 03, 2013 at 12:43 AM · androidjumpjumping

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); 


             }
Comment
Add comment · Show 8
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Benproductions1 · Apr 03, 2013 at 12:52 AM 0
Share

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 :)

avatar image Benproductions1 · Apr 03, 2013 at 12:52 AM 0
Share

Just reading through the comments in the code gives you the answer

avatar image Bisonator · Apr 03, 2013 at 01:04 AM 0
Share

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?

avatar image Benproductions1 · Apr 03, 2013 at 01:08 AM 0
Share

The problem is probably because you are calling $$anonymous$$ove() more than once. You should never do this, ever

avatar image Benproductions1 · Apr 03, 2013 at 02:58 AM 1
Share

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

Show more comments

3 Replies

· Add your reply
  • Sort: 
avatar image
-1

Answer by el_rolas · Apr 06, 2013 at 04:19 AM

moveDirection.y += jumpSpeed;

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Benproductions1 · Apr 06, 2013 at 04:33 AM 0
Share

2 things wrong with this answer.

  1. It's wrong, it will yield a different result every time you jump

  2. 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

avatar image
0

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

Comment
Add comment · Show 13 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bisonator · Apr 03, 2013 at 12:10 PM 0
Share

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.

avatar image Bisonator · Apr 03, 2013 at 12:12 PM 0
Share

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?

avatar image Benproductions1 · Apr 03, 2013 at 07:19 PM 0
Share

Sorry, forgot to add that

avatar image Bisonator · Apr 03, 2013 at 08:17 PM 0
Share

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.

avatar image Benproductions1 · Apr 03, 2013 at 09:23 PM 0
Share

right now direction is relative to the camera, if yyou want it relative to the player make the nessesary changes to line 40

Show more comments
avatar image
0

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,?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges