Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Z-ion · Jul 10, 2019 at 10:25 AM · c#physicsrigidbodyvelocitymovement script

Moving a player with Rigidbody

Hello everyone, I want to create a physics based movement, I am also using unity for quite sometime and always use Rigidbody.velocity or Rigidbody.MovePosition to move the player.

First thing I want is to move the player with tight controls example when user press button movement key it starts moving instantly and when released it should stops instantly too. Second thing I want is to add force in certain direction when user press ability key.

First I went with MovePosition but it doesn’t change the actual velocity of the player. Then I try to change the velocity of the player directly, but that’s where I meet with a problem as when player is not pressing the movement key it’s velocity is zero and AddForce doesn’t work when user press ability key.

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 Ak0rn · Jul 10, 2019 at 10:58 AM

The below code should do what you want.

public RigidBody playRB; //Put your player's rigidBody in here public float runSpeed;

float movement = Input.GetAxis("Horizontal"); if (movement > 0 || movement < 0) { ; playRB.velocity = new Vector3(movement * runSpeed, playRB.velocity.y, 0); }

Comment
Add comment · Show 5 · 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 Z-ion · Jul 10, 2019 at 02:33 PM 0
Share

Thanks mate just one doubt: If I press the key player gets some velocity and when key is released it’s velocity will be not be zero, if I put else statement on which velocity will be zero then I will not be able to AddForce as it get zero when key is not pressed.

avatar image Ak0rn Z-ion · Jul 10, 2019 at 05:16 PM 0
Share

What is the condition required for the addForce part?

avatar image Z-ion Ak0rn · Jul 11, 2019 at 02:22 PM 0
Share

So if you are moving in a certain direction and press the ability key it add force in that direction but if you are not moving then I want to add force in the direction player is facing and sometimes in opposite direction.

Show more comments
avatar image
0

Answer by Z-ion · Jul 13, 2019 at 01:11 PM

This is the code I am using

     void Update()
     {
       float x = Input.GetAxisRaw(“Horizontal”);
       float z = Input.GetAxisRaw(“Vertical”);
     
       if(x!=0 || z!=0)
     {
       Vector3 movement = new Vector3(x,0,z).normalize * speed;
       Vector3 yTemp = new Vector3(0,rb.velocity.y,0);
       rb.velocity = movement;
       rb.velocity += yTemp;
      }
     
     if(Input.KeyDown(KeyCode.Space)) 
       rb.AddForce(transform.forward * 100);
     }

When I move with the WASD key then it works but as soon as I let go the key the velocity doesn’t go to zero and move in that direction. Also if I remove IF statement then when I press Space key to add force it add but in next frame it becomes zero again and don’t have any effect

Can someone help me out and thanks in advance.

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 suman818 · Jul 14, 2019 at 08:43 AM 0
Share

Input.Get$$anonymous$$eyDown() returns true when you press it and in each frame, it resets its value and it will go to False state. so you need to press it again.. ins$$anonymous$$d use Input.GetAxis() and keep the button pressed for continuous force

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

151 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

Related Questions

Unity relative velocity limiting 0 Answers

RigidBody clips into corners 3 Answers

Rigidbody character controller can't walk on stairs 0 Answers

Problem Trying to Apply Non-Kinematic Velocity to Rigidbody 0 Answers

GameObject disappears when velocity is set to (near) zero. 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