• 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 sunydark · Nov 30, 2012 at 12:57 AM · jumpingmovinggroundeddoublejump

Error jumping and moving

I'm having trouble with the drive system with perssonagens script in C #. I use the following code:

private Vector3 moveDirection = Vector3.zero; private bool grounded = false;

public float speed = 10.0f; public float jumpSpeed = 8.0f; public float gravity = 20f; public int countJump = 0; public int maxJumps = 2;

void OnCollisionEnter(){ countJump = 0; }

void FixedUpdate () { if(!grounded){ moveDirection.x = Input.GetAxis("Horizontal")*speed; moveDirection.z = Input.GetAxis("Vertical")*speed; }else{ moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); countJump = 0;

} if(countJump < maxJumps){ if(Input.GetButtonDown("Jump")){ moveDirection.y = jumpSpeed; countJump ++; } } moveDirection = transform.TransformDirection(moveDirection);

moveDirection.y -= gravity * Time.deltaTime;

     CharacterController controller = (CharacterController)GetComponent(typeof(CharacterController));
     CollisionFlags flags = controller.Move(moveDirection * Time.deltaTime);
     grounded = (flags & CollisionFlags.CollidedBelow) != 0;

if (Input.GetKey (KeyCode.LeftShift)){ speed = 23.0f;

}else{ speed = 10.0f; } } }

some sample images:

1: http://imageshack.us/photo/my-images/585/90786173.png/

2: http://imageshack.us/photo/my-images/854/84060635.png/

I tried both ways but did not get a good result. 1> Slow Motion, High Jump. 2> Move fast, Jump Low; What should I do?

Sorry my bad english!!

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Goliath520 · Nov 30, 2012 at 08:54 AM

Unfortunatly I don't have a solution for you. I have been looking over it for a little bit and don't know what is wrong. I did however clean up the code so it is a little easier to read.

 private Vector3 moveDirection = Vector3.zero;
 private bool grounded = false;
 
 public float speed = 10.0f; 
 public float jumpSpeed = 8.0f; 
 public float gravity = 20f; 
 public int countJump = 0; 
 public int maxJumps = 2;
 
 void OnCollisionEnter()
 { 
     countJump = 0; 
 }
 
 void FixedUpdate () 
     { 
     if(!grounded)
         { 
             moveDirection.x = Input.GetAxis("Horizontal")*speed;
             moveDirection.z = Input.GetAxis("Vertical")*speed; 
         }
     else
         { 
             moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
             countJump = 0;
         } 
         
     if(countJump < maxJumps)
     { 
         if(Input.GetButtonDown("Jump"))
         {
             moveDirection.y = jumpSpeed; countJump ++; 
         } 
     }     
     moveDirection = transform.TransformDirection(moveDirection);
     moveDirection.y -= gravity * Time.deltaTime;
     CharacterController controller = (CharacterController)GetComponent(typeof(CharacterController));
     CollisionFlags flags = controller.Move(moveDirection * Time.deltaTime);
     grounded = (flags & CollisionFlags.CollidedBelow) != 0;
 
     if (Input.GetKey (KeyCode.LeftShift))
     {
         speed = 23.0f;
     }else
     {
         speed = 10.0f;
     } 
 }
 }

The other thing that isn't a big deal but you can use to avoid extra work is using && in an if statement to avoid unnecessary nesting. I am refering to the jump portion of the code where you used:

 if(countJump < maxJumps)
     { 
         if(Input.GetButtonDown("Jump"))
         {
             moveDirection.y = jumpSpeed; countJump ++; 
         } 
     }     

Instead it might be easier to use:

 if(Input.GetButtonDown("Jump") && countJump < maxJumps)
     { 
         moveDirection.y = jumpSpeed; countJump ++; 
     } 

Hopefully someone can answer this for you. If I figure it out I will comeback and respond.

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
avatar image
0

Answer by sunydark · Dec 01, 2012 at 12:22 AM

Thanks Go, now works as expected.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

11 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

Related Questions

Player can jump on invisible non ground items 1 Answer

How to check for the ground in Unity 2D for Jumping 1 Answer

How to jump on only "ground" colliders 1 Answer

Multiple jump C# Script. 0 Answers

CharacterController - moving while jumping (movement vector values reset?) 1 Answer


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