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
1
Question by DoYouEvenFish · Jul 15, 2017 at 03:03 AM · rigidbodyvelocity

Using rigidbody to move the player

I'm trying to use rigidbody.velocity to move my player, however it doesn't seem to work at all. Here is the rigidbody settings on the player: alt text Here are the input settings: alt text And here is the file controlling player movement:

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

 public class PlayerController : MonoBehaviour {

     public Rigidbody rb;


     void Start () {
         rb = GetComponent<Rigidbody>();

     }

     void Update () {
         Vector3 desiredVelocity = Input.GetAxis("Horizontal") * transform.right + Input.GetAxis("Vertical") * transform.forward;
         rb.velocity = Vector3.Lerp(rb.velocity, desiredVelocity, Time.deltaTime * 5f);

     }
 }

Can someone explain to me why this doesn't work?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by Gamer206 · Jul 25, 2017 at 12:46 PM

Use this:

 Rigidbody rb;
 public float speed;
 
 void Start () {
 rb = GetComponent<Rigidbody>();
 }
 void FixedUpdate () {
 float mH = Input.GetAxis ("Horizontal");
 float mV = Input.GetAxis ("Vertical");
 rb.velocity = new Vector3 (mH * speed, rb.velocity.y, mV * speed);
 }

Comment
Add comment · Show 2 · 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 Smartoom7 · Aug 09, 2020 at 05:24 PM 0
Share

This will make troubles with diagonal moving, the diagonal moving is going to be faster than vertical or horizontal.

avatar image Chris_A_75 · Jan 12, 2021 at 10:01 PM 0
Share

Is there a way of making the rigidbody move where the camera is facing? Because I'm trying to use this script on a first person game and the Player is only moving in 4 directions only

avatar image
0

Answer by TWicked · Jul 15, 2017 at 03:29 AM

You can use this code to move player

     void FixedUpdate ()
     {
         float moveHorizontal = Input.GetAxis ("Horizontal");
         float moveVertical = Input.GetAxis ("Vertical");
 
         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
 
         rb.AddForce (movement * speed);
     }

https://unity3d.com/learn/tutorials/projects/roll-ball-tutorial/moving-player?playlist=17141

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 SilverScales · Jun 13, 2018 at 08:52 PM

Is there no way to move the player in a "snappy" fashion, without the slow responsiveness?

Comment
Add comment · Show 3 · 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 Victorhs98 · Sep 10, 2018 at 03:36 AM 1
Share

$$anonymous$$aybe this could help: float h = Input.GetAxis("Horizontal");

             if(h * GetComponent<Rigidbody2D>().velocity.x < maxSpeed)
             {
                 rb.AddForce(Vector2.right * h * moveForce);
             }
 
             if($$anonymous$$athf.Abs(GetComponent<Rigidbody2D>().velocity.x) > maxSpeed)
             {
                 rb.velocity = new Vector2($$anonymous$$athf.Sign(GetComponent<Rigidbody2D>().velocity.x) * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);
             }
avatar image Smireles · Aug 04, 2019 at 06:38 PM 0
Share

Better later than never. SilverScales, the slow responsiveness is because the examples above are using an Axis input, those go from -1 to 1 (where 0 is the resting value) interpolation. You can just normalize those numbers and go using rounded values to get the snappy movement you mentioned.

avatar image Cornelis-de-Jager · Aug 04, 2019 at 09:48 PM 1
Share

Yes absolutely, allot of the people here are using Input.GetAxis("Horizontal") or Vertical. These are actually predefined inputs. Here is the thing, you can edit these inputs or even create your own.

CLIC$$anonymous$$ $$anonymous$$E TO SEE HOW

Allot of the inputs have a growth or decay factor. $$anonymous$$eaning how quickly they ramp up or slow down. Great for realistic movement. But if you want snappy then simply set the growth the same speed as the boundries. That will give you instant movement and stopping.

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

94 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

Related Questions

Velocity powered rigidbody on a moving platform without parenting. 3 Answers

Rigidbody cube randomly stops moving? 1 Answer

Rigidbody movement conflict? 1 Answer

Simulate a ball bouncing off of a linecast? 0 Answers

Getting the player to move with platform: Velocity, DistanceJoint, or...? 2 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