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 glennuke · Nov 29, 2020 at 11:25 PM · movementslowdown

How can i get my player to slow down when i stop moving?

My code

 using System.Collections;
 using System.Collections.Generic; 
 using UnityEngine;
 
 [RequireComponent(typeof(Rigidbody))] public class PlayerController : MonoBehaviour {
     public Rigidbody Player;
     public Camera Camera;
     public Vector3 jump;
     public float jumpForce = 2.0f;
 
     public bool isGrounded;
     Rigidbody rb;
     void Start(){
         rb = GetComponent<Rigidbody>();
         jump = new Vector3(0.0f, 2.0f, 0.0f);
         Cursor.visible = false;
         Cursor.lockState = CursorLockMode.Locked;
     }
 
     void OnCollisionStay(){
         isGrounded = true;
     }
 
     // Update is called once per frame
     void Update()
     {
         if (Input.GetKey("d")) 
         {
             Player.AddRelativeForce(4, 0, 0);
         }
 
         if (Input.GetKey("a")) 
         {
             Player.AddRelativeForce(-4, 0, 0);
         }
 
         if (Input.GetKey("w")) 
         {
             Player.AddRelativeForce(0, 0, 4);
         }
 
         if (Input.GetKey("s")) 
         {
             Player.AddRelativeForce(0, 0, -4);
         }
 
         if (Input.GetKeyDown(KeyCode.Space) && isGrounded){
 
              rb.AddForce(jump * jumpForce, ForceMode.Impulse);
              isGrounded = false;
     }  } }




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 DinoMax0112 · Nov 30, 2020 at 02:41 PM

Hello, I hope this helps.

using System.Collections; using System.Collections.Generic; using UnityEngine;

     public class PlayerController : MonoBehaviour
     {
         public float speed = 10.0f;
         public Rigidbody rb;
         public Vector2 movement;
     
         // Use this for initialization
         void Start()
         {
             rb = this.GetComponent<Rigidbody>();
         }
     
         // Update is called once per frame
         void Update()
         {
             movement = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
         }
         void FixedUpdate()
         {
             moveCharacter(movement);
         }
         void moveCharacter(Vector2 direction)
         {
             rb.MovePosition((Vector2)transform.position + (direction * speed * Time.deltaTime));
         }
  if (Input.GetKeyDown(KeyCode.Space) && isGrounded){
  
               rb.AddForce(jump * jumpForce, ForceMode.Impulse);
               isGrounded = false;
     }
 }

What this is doing instead of add force is it is moving the rigid body instead of the rigid body getting a separate source and having to wait to slow down.

If you have any problems with it leave a coment and I'll come back to troubleshoot.

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 glennuke · Dec 05, 2020 at 04:45 PM 0
Share

Assets\PlayerController.cs(36,2): error CS1022: Type or namespace definition, or end-of-file expected

Assets\PlayerController.cs(34,27): error CS1519: Invalid token '=' in class, struct, or interface member declaration

Assets\PlayerController.cs(33,64): error CS1519: Invalid token ';' in class, struct, or interface member declaration

Assets\PlayerController.cs(33,27): error CS1519: Invalid token '(' in class, struct, or interface member declaration

Assets\PlayerController.cs(31,52): error CS1519: Invalid token ')' in class, struct, or interface member declaration

Assets\PlayerController.cs(31,39): error CS1519: Invalid token '&&' in class, struct, or interface member declaration

Assets\PlayerController.cs(31,37): error CS8124: Tuple must contain at least two elements.

Assets\PlayerController.cs(31,23): error CS1026: ) expected

Assets\PlayerController.cs(31,23): error CS8124: Tuple must contain at least two elements.

Assets\PlayerController.cs(31,3): error CS1519: Invalid token 'if' in class, struct, or interface member declaration

avatar image
0

Answer by CmdrZin · Dec 05, 2020 at 08:40 PM

Two possible ways.
1. Increase the Drag parameter for the Player RigidBody in the Inspector to 0.8 or so to add drag.
2. Simplify your movement control like this.

     // Player movement control
     void FixedUpdate()
     {
         Vector3 currVelocity = playerRb.velocity;
 
         velocity.y = Input.GetAxis("Vertical") * speed * Time.deltaTime;
         velocity.x = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
         //        transform.Translate(velocity.x, 0, velocity.y);
         playerRb.AddRelativeForce(new Vector3(velocity.x, 0f, velocity.y) * speed);
 
         if(playerRb.velocity.magnitude > 1)
         {
             // Don't modify.
             playerRb.velocity = currVelocity;
         }
     }

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

206 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 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 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 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 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 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 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 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 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 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

Slow Character Movement 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Making a bubble level (not a game but work tool) 1 Answer

Character input key doesnt work 1 Answer

4 way direction movement using a Character Controller. 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