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 Ostinyo · Mar 10, 2014 at 05:43 AM · movementcharactercontrollergravity

Character Controller Stops Moving Forward in Mid Air

Note: I am using the First Person Character Controller (removed characterMotor.js and other control scripts).

I am having a problem with my automatic control system (this is for an infinite running game). When my character controller's vertical velocity reaches 0.0 while falling, my controller stops moving forward on the Z axis (default speed is 6.0 forward) although the moveDirection.z is still 6. I would like to be able to clean up my code to be more simplistic, as my character can only run in one direction and doesn't stop (unless it hits a wall).

Code: (undefined variables are defined before Update)

 function Update()
 {
     //can I use strictly Move instead?
     var forward : Vector3 = transform.TransformDirection(Vector3.left);
     var curSpeed : float = speed * Input.GetAxis ("Vertical");
     transform.Rotate(0,0,Input.GetAxis ("Horizontal") * rotateSpeed);
     transform.Rotate(0,0,Input.GetAxis ("Unhorizontal") * -rotateSpeed);
 
     controller.SimpleMove(forward * curSpeed); //move player

     //++STANDARD SETTINGS++\\
     if (!isClimbing && !isSliding && !isRolling && !isTumbling && !isDead && !isHanging && isBalancing == 0)
     {
         moveDirection.z = 6.0; //run forward
     }
     if (controller.isGrounded && !isJumping)
     {
         Physics.gravity.y = -9.81;
         isJumping = false;
         moveDirection.y = -0.05; //keeps the controller grounded
     }
     else if (!controller.isGrounded && !isClimbing && !isHanging && !isDead)
     {
         isJumping = true;
         Physics.gravity.y = -9.81;
         moveDirection.y += Physics.gravity.y*Time.deltaTime;
     }
     
     //++JUMPING++\\ (swipe up --> moveDirection.y = 8.0 and isJumping = true)
     if(isJumping)
     {
         if (Physics.Raycast (transform.position, Vector3.up, hit, 1.2))
         {
             moveDirection.y = 0;
         }
     }
 
     controller.Move(moveDirection * Time.deltaTime); //move player minor
 }

Sorry if the code is confusing; I'm omitting hundreds of lines to focus on the main character movement. Any help would be greatly appreciated!

P.S. I would love suggestions for a better way to control the player- modifying moveDirection often causes problems!

Comment
Add comment · Show 3
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 nicolasjr · Mar 10, 2014 at 08:49 PM 0
Share

Well, first of all, I think that the best way of handling endless runner would be to keep the player still and move the assets, always co$$anonymous$$g in direction of the camera. That would be helpful for you in a fist moment. For floor, walls and stuff like that, you can simply change the texture's offset and it'll look like the player is moving.

Also, there are some IF statements that are verifying a lot of conditions. That makes the code very hard to understand. It's very possible that you're setting wrong a specific flag in a given moment. You should try to simplify it, reducing the amount of control variables you're using.

Other than that, you're setting the gravity in many different places, and always with the same value. Also, inside the update method. You could set it outside the update $$anonymous$$ethod, since the value is not being changed.

I know I didn't provided you a specific answer to your question, but, perhaps, if you think about my suggestions, it'll help you to find the problems easier in the future.

avatar image Ostinyo · Mar 11, 2014 at 01:51 AM 0
Share

I'll consider that, thanks! However, I do allow the player to strafe right/left and occasionally turn 90 degrees. Does having so much code in the Update function cause a significant drop in performance?

avatar image nicolasjr · Mar 11, 2014 at 11:05 AM 0
Share

well, to be honest, it's not much about performance, but it's not much elegant to handle this things I'm update. I suggest you to use events from an input handler and handle the behaviors in different methods.

for instance, in an input handler class, you detect a swipe to right. now you send the event and read it in the player behavior class, and make the player move to right. Does that makes sense?

I have a endless runner player behavior where I don't even use update method.

0 Replies

· Add your reply
  • Sort: 

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

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

Related Questions

Character Controller / Charactor Motor has no inputs? 1 Answer

Charactercontroller won't go down, only goes up 0 Answers

Round Planets and Movement on them 2 Answers

Movement with character controller on different walls 0 Answers

How to change the angle my character falls at 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