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 /
This question was closed Apr 12, 2012 at 07:53 PM by SrBilyon for the following reason:

Self Solved

avatar image
1
Question by SrBilyon · Apr 01, 2012 at 05:56 AM · accelerationslidingmomentuminertia

Accelerating and Decelerating a Characters's speed

I'm looking to have a character accelerated when moving forward, decelerate when movement keys aren't pressed (keyboard/joystick), and slide when on certain surfaces.

I have managed to accelerate the player, but I'm having issues with the deceleration. I assume is because I'm multiplying the moveDirection Vector's forward and right (z and x) by Input.GetAxis (which becomes 0 when they are let go.)

After that I'll know how to code in the friction.

I've attached the segments of my player script that deals with movement exclusively. This is for a 3D platformer by the way, similar to Mario 64/Galaxy.

Any help would be wonderful. Thanks in advance!

  //=====================================================================
 // CalculateMovement
 //=====================================================================
 function CalculateMovement()
 {
     var jumpButton = Input.GetButton("Jump");
     var jumpButtonPressed = Input.GetButtonDown("Jump"); 
     var dodge = Input.GetKeyDown(KeyCode.DownArrow);
     
     x  = Input.GetAxis("Horizontal");
     z  = Input.GetAxis("Vertical"); 
     y  = moveDirection.y;    
     
     if (x != 0 || z != 0)
     isMoving = true;
     
     else if (x == 0 && z == 0)
     isMoving = false;
     
     horizontalSpeed = horizontalVelocity.magnitude;
     verticalSpeed = controller.velocity.y;
     overallSpeed = controller.velocity.magnitude;
 
     var cameraTransform : Transform  = Camera.main.transform;
 
     var forward : Vector3 = cameraTransform.TransformDirection(Vector3.forward);
     forward.y = 0;
     forward = forward.normalized;
     
     var right = Vector3(forward.z, 0, -forward.x);
     
     if ((!isAttacking) && canMove && isMoving)
     {    
         moveDirection = x * right + z * forward;
     }
 
     if (moveDirection != Vector3.zero)
     {
         curSpeed += speed * Time.deltaTime;
         curSpeed = Mathf.Clamp(curSpeed,0,maxSpeed);    
     }
 
     if (!isMoving && canMove)
     {
         curSpeed -= (friction) * Time.deltaTime;
         
         if (curSpeed < 0)
             curSpeed = 0;    
     }
 }
 
 //=====================================================================
 // ApplyMovement
 //=====================================================================
 function ApplyMovement()
 {
          
     // Move the controller
     if (!busy))
     {
         var recalcMoveDir : Vector3 = Vector3(moveDirection.x,0,moveDirection.z)*curSpeed;
         recalcMoveDir.y = moveDirection.y;
         
         controller.Move((recalcMoveDir) * Time.deltaTime); 
     }
 }
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

1 Reply

  • Sort: 
avatar image
0

Answer by Bunny83 · Apr 01, 2012 at 08:17 PM

Well, the CharacterController has been invented so it's not affected by momentum or other external forces. If you want it to behave like a physic object you might want to use a Rigidbody instead. A Rigidbody can be moved via AddForce and it's automatically affected by friction and gravity. The friction can be modified by the PhysicMaterial of the surface.

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 SrBilyon · Apr 01, 2012 at 09:55 PM 0
Share

Thanks. I'm aware that CharacterControllers aren't affected by physics forces naturally. I intended to code in formulas for when I wanted to create my own Physics (which I really only need for making the character accelerate, decelerate, and be affected by more, less or no friction). I'm using a character controller for the sake that it makes collisions in my game easier.

I've updated my code above so that it includes acceleration, but now I 'm having problems with the deceleration, which I assume is because I'm multiplying the moveDirection vector forward and right (z and x) by axis (which becomes 0 when they are let go.)

After that I'll know how to code in the friction.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

rigidbody2D with instant max speed and slow stop 1 Answer

How to decrease inertia for a rolling ball 1 Answer

What can I do to prevent momentum? 0 Answers

Character Movement 2 Answers

Spaceship movement with acceleration and deceleration on Unity 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